seeker
seeker

Reputation: 6991

How to tokenize a set of lines from a given input?

I'm trying something pretty simple.

I have to read the following input line by line and create an ArrayList of Integers for every line.

2 5 10
3 5 15

I'm trying to process it using the following code

while((newLine=f.readLine()) != null){
    for(int k=0; k<newLine.length(); k++){
        StringTokenizer st = new StringTokenizer(newLine);
        int i1 = Integer.parseInt(st.nextToken()); 
        System.out.println("Adding to list"+i1);
        input.add(i1);
    }
}

As you can see, I'm reinitializing st to newLine each time, instead of moving onto the next token. Any thoughts on how I can achieve what I am intending to?

Upvotes: 2

Views: 179

Answers (3)

Reimeus
Reimeus

Reputation: 159754

You're adding the first token to the ArrayList multiple times in the for loop. Just iterate over the tokens:

while ((newLine=f.readLine()) != null){
   StringTokenizer st = new StringTokenizer(newLine);
   while (st.hasMoreTokens()) {
      int number = Integer.parseInt(st.nextToken()); 
      input.add(number);
   }
}

Upvotes: 1

A4L
A4L

Reputation: 17595

You do not need to iterate over the line length. You need instead to iterate over all the tokens returned by the tokenizer and put them in a list, after you are done with one line put that list in an other list, so you end up with a list of lists

List<List<Integer>> linesOfIntegers = new ArrayList<>(); 

while((newLine = f.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(newLine);
    List<Integer> lineOfIntegers = new ArrayList<>();
    while(st.hasMoreTokens()) {
        lineOfIntegers.add(Integer.parseInt(st.nextToken()));
    }
    linesOfIntegers.add(lineOfIntegers);
}

for(List<Integer> lineOfIntegers : linesOfIntegers) {
    System.out.println(lineOfIntegers);
}

Output

[2, 5, 10]
[2, 5, 15]

Upvotes: 1

shiladitya
shiladitya

Reputation: 2310

The tokenizer statement must be directly in outer loop.

while((newLine=f.readLine())!=null){
     StringTokenizer st = new StringTokenizer(newLine);  //this must be once for every line. not for every character in each line
     while(st.hasNext()){

         int i1 = Integer.parseInt(st.nextToken()); 
         System.out.println("Adding to list"+i1);
         input.add(i1);
     }
}

Upvotes: 1

Related Questions