I must be missing something obvious about how the various Scanner methods work but this really doesn’t make any sense:
this code causes an infinite loop:
Scanner valueScan = new Scanner(line);
valueScan.useDelimiter(",");
while(valueScan.hasNext())
{
count++;
}
while this code is valid:
Scanner valueScan = new Scanner(line);
valueScan.useDelimiter(",");
while(valueScan.hasNext())
{
System.out.println("value token: " + valueScan.next());
cellCount++;
}
This is from Oracle’s documentation I am not sure if it applies or what it means: “This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true. ” (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#next%28%29)
Because
hasNextdoesn’t remove anything from theScanner, so in your first case, you never change its state, hence the infinite loop.nextextracts input, and hence changes the state of theScanner.