views:

52

answers:

3

Hello everyone. 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"));

            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

+2  A: 

You have "return tokens;" in your while loop. Seems like that early return would effectively cut off processing on the first line.

Jeremy Goodell
tried that. and it didnt work
Luron
thank you though
Luron
@Luron - Seriously, the function will exit at the `return` statement. That would be a reason why it only reads one line.
Greg
ugh. so i have to redo this whole thing
Luron
A: 

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.

Greg
@Luron - See my edit. It would help if you were more clear in your question. What is the required behavior? What does the input look like? In what ways do each of the suggestions on this page "not work"?
Greg
A: 

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).

xagyg
You may want to convert to a Set if you want unique tokens. If you don't want to add all tokens, but want to set line by line then tokens = Arrays.asList(l.split(" ")); // depends what you want
xagyg