0
Scanner method
I want to count the numbers of inputs with the help of the scanner method hasNextInt(). My inputs are 1 2 3 1 3 5 2 and the value of the variable counter should be 7. When I start the programm, I get an infinite loop. Why am I getting an infinite loop? For example: int counter=0; while(sc.has.NextInt()) { counter++; } System.out.println(counter);
2 Respostas
+ 3
because checking if it has a next integer doesn't jump to the next one
while (sc.hasNextInt()) {
counter++;
sc.nextInt();
}
+ 1
Now the programm works without any problem. Thank you