what do i add to remove line breaks?

Hi. i have a code (perhaps not the most beautiful one but anyway), it works- so far so good. what it does is that it opens a file, reads it, finds words 1-3, 2-4... from the first line, what it can't do is to read from the second. how do i change it in order for it to do so?

import java.io.*;

import java.util.*;

import javax.swing.*;

import java.util.regex.Pattern;

import java.util.regex.Matcher;

import java.lang.*;

import javax.swing.*;

public class LasFil {

public static void main(String[] args) throws IOException {

String s;

WordExtractor b;

String c = " ";

String d;

WordExtractor m;

String f;

String g;

WordExtractor h;

String i;

String j;

String k;

String l;

try{

FileReader fr = new FileReader( new File("test.txt"));

BufferedReader br = new BufferedReader(fr);

while ( null != ( s=br.readLine()) ) {

while (c != ".") {

b = new WordExtractor(s);

c = b.getFirst();

d = b.getRest();

m = new WordExtractor(d);

f = m.getFirst();

g = m.getRest();

h = new WordExtractor(g);

i = h.getFirst();

j = h.getRest();

k = c + f + i;

l = c + " " + f + " " + i;

System.out.println("The first three words are: " + l);

s = b.getRest();

}

//nytt

System.out.println(s);

}

}

catch( Exception e )

{

}

}

}

[1504 byte] By [SandraPandraa] at [2007-11-27 4:07:16]
# 1

I find your explanation of what you are trying to do hard to follow.

One error at least in your code is that you do not use == to compare Strings (or other Objects) for equality in Java. You must use the equals method. That may or may not be your problem but it probably isn't helping.

See [url=http://java.sun.com/docs/books/tutorial/java/data/comparestrings.html]http://java.sun.com/docs/books/tutorial/java/data/comparestrings.html[/url]

cotton.ma at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 2
the problem is that this one stops reading after line break. i would like to be able to read several lines.
SandraPandraa at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 3
> the problem is that this one stops reading after line> break. i would like to be able to read several lines.Never use empty catch blocks. Do at least print the exception.Kaj
kajbja at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 4
Do also use code formatting when you post code.. http://forum.java.sun.com/help.jspa?sec=formattingLycka tillKaj
kajbja at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 5
tack :)
SandraPandraa at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 6
> tack :)Did you see the post by cotton.m?You should not use == or != to compare strings. use equals instead. The while loop:while (c != ".") {will never exit since c always will be != from "."Kaj
kajbja at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 7

i found another code that read several lines but after i had added the wordextractor part of the code (the getFirst and getRest) it only read the first line again. ...so that's probably where my problem is.

gonna go and study what "posBlank" and "sentence.indexOf" really implies.

thanks for helping me guys!

SandraPandraa at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 8

> i found another code that read several lines but

> after i had added the wordextractor part of the code

> (the getFirst and getRest) it only read the first

> line again. ...so that's probably where my problem

> is.

>

> gonna go and study what "posBlank" and

> "sentence.indexOf" really implies.

>

> thanks for helping me guys!

The problem with your code is probably that the code throws an exception. Print the stacktrace and correct the code.

Kaj

kajbja at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 9

> > tack :)

>

> Did you see the post by cotton.m?

>

> You should not use == or != to compare strings. use

> equals instead.

>

> The while loop:

>

> while (c != ".") {

>

> will never exit since c always will be != from "."

>

> Kaj

good to know!

SandraPandraa at 2007-7-12 9:12:32 > top of Java-index,Java Essentials,Java Programming...
# 10
kaj, since you're obviously familiar with swedish, what does "stacktrace" mean?
SandraPandraa at 2007-7-12 9:12:33 > top of Java-index,Java Essentials,Java Programming...
# 11
have to hand over the computer to the owner of it for a couple of hours, will be back as soon as i can
SandraPandraa at 2007-7-12 9:12:33 > top of Java-index,Java Essentials,Java Programming...
# 12

> kaj, since you're obviously familiar with swedish,

> what does "stacktrace" mean?

Here's an example program:

class Test {

public static void main(String[] args) {

try {

//Some code that causes an exception

String nullReference = null;

nullReference.toString();//Will cause a null pointer exception

} catch (Exception e) {

e.printStackTrace();//This prints the stacktrace

}

}

}

This is printed when you execute the program:

java.lang.NullPointerException

at Test.main(Test.java:6)

The text above is the example of a stacktrace. It describes the error that have occurred (in this case NullPointerException) and it says where it has occured (in this case in the file Test.java at line 6, in the main method)

And yes, I'm Swedish so I can explain it in Swedish if you want to.

Kaj

kajbja at 2007-7-12 9:12:33 > top of Java-index,Java Essentials,Java Programming...