The input file that I am trying to read contains the amount of lines for a block of entries on the first line, and records on the subsequent lines. The file may contains many blocks like this. For example:
3
a b c
a b
a c b //Three in the first line tells that there are 3 subsequent lines.
2
a b
a b c
0 //the input ends here
I am trying to use a while loop to read one block at a time, process it, and then read in another block. However the code works only when there is one block. When there are multiple blocks, it freezes.. Can someone help to figure out what is wrong? Thank you!
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
while (s.hasNextLine()){
String number = s.nextLine();
if (line.equals("0")){
break;
}else {
int n = Integer.parseInt(number);
for (int i=0; i<n; i++){
String letters= s.nextLine();
...
//do something to store the lines of letters
}
}
}
}
if (!line.equals("0"))Shouldn't that beif (line.equals("0"))?ifstatement does.s.nextLine()to actualy advance in the file.