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 )
{
}
}
}
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]
> 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
> 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