views:

71

answers:

1

alright so I am trying to read a text file. and then split it up into lines which actually represent processes in a process table arraylist. then i want to split up all the tokens in the line and make all the tokens of the line an arraylist again so far this is all i have:

its not working correctly. i am getting:

processes:[[netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF,   011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF]]
processes:[[netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF, 011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF, textpad, 2, 391BCA, 871BAF, DEA14C, EEFC30, 000000, 000000, 0000AA, AF, 000000, 000000, 000000, 000000, 000000, FFFFFF, B4344D, 000000], [netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF, 011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF, textpad, 2, 391BCA, 871BAF, DEA14C, EEFC30, 000000, 000000, 0000AA, AF, 000000, 000000, 000000, 000000, 000000, FFFFFF, B4344D, 000000]]

BufferedReader inputStream = null;
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
ArrayList<String> tokens = new ArrayList<String>();
/* read the file*/
try {
    inputStream = new BufferedReader(new FileReader("p56.txt"));

    while (true) {
        /* while the file was read*/
        /* now. split the file into the lines.*/
        String line = inputStream.readLine();
        if (line == null) {
            break;
        }
        //if there are no more lines left. break

        // split the lines into tokens and make into an arraylist*/
        Scanner tokenize = new Scanner(line);
        while (tokenize.hasNext()) {
            /*while there are still more*/
            tokens.add(tokenize.next());
        }
        lines.add(tokens);

        System.out.println("processes:" + lines);
    }
}
+1  A: 

You need to move the line

ArrayList<String> tokens = new ArrayList<String>();

to right before

while (tokenize.hasNext()) {

It will then create a new list of tokens before processing each line. Otherwise you will end up with a list of all tokens for all lines of the file.

BalusC
YES!!!!!! thank you very much
Luron
You're welcome :)
BalusC
how exciting! thank you. now how can I access each process's tokenized arraylist now?
Luron
Iterate over `lines` using a [`for` statement](http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.html). If you stucks, ask a new question.
BalusC