I am having difficulties with a program that I have been working on all day. I am trying to read a text file and read each line one at a time. Take that line and make an arraylist of the words of the line. then using the index of the arraylist define terms with it.
public class PCB {
public static void main(String arg[]) {
read();
}
public static ArrayList read() {
BufferedReader inputStream = null;
ArrayList<String> tokens = new ArrayList<String>();
try {
inputStream = new BufferedReader(new FileReader("processes1.txt")开发者_Go百科);
String l;
while ((l = inputStream.readLine()) != null) {
Scanner tokenize = new Scanner(l);
while (tokenize.hasNext()) {
tokens.add(tokenize.next());
}
return tokens;
}
} catch (IOException ioe) {
ArrayList<String> nothing = new ArrayList<String>();
nothing.add("error1");
System.out.println("error");
//return nothing;
}
return tokens;
}
}
The error I am getting is it only reads the first line. What am I doing wrong? Thank you so much in advance
You have "return tokens;" in your while loop. Seems like that early return would effectively cut off processing on the first line.
Try changing your loop to the following. Note how I moved the return statement.
while ((l = inputStream.readLine()) != null) {
Scanner tokenize = new Scanner(l);
while (tokenize.hasNext()) {
tokens.add(tokenize.next());
}
}
return tokens; // <-- outside the loop
Edit: If you want to read the entire file and store the tokens of each line in a seperate array, then you could create an ArrayList
of ArrayList
.
public static ArrayList<ArrayList<String>> tokenizeFile(string filename) {
BufferedReader inputStream = new BufferedReader(new FileReader(filename));
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
while (true) {
String line = inputStream.readLine();
if (line == null) break;
ArrayList<String> tokens = new ArrayList<String>();
Scanner tokenizer = new Scanner(line);
while (tokenizer.hasNext()) {
tokens.add(tokenizer.next());
}
lines.Add(tokens);
}
return lines;
}
Note: My Java is rusty.
Simplify to this ...
String l;
while ((l = inputStream.readLine()) != null) {
tokens.addAll(Arrays.asList(l.split(" ")));
}
... creates a list of all tokens on all lines in the file (if that is what you want).
精彩评论