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);

30th Oct 2019, 5:25 PM
Preet
2 ответов
+ 3
because checking if it has a next integer doesn't jump to the next one while (sc.hasNextInt()) { counter++; sc.nextInt(); }
30th Oct 2019, 5:30 PM
Airree
Airree - avatar
+ 1
Now the programm works without any problem. Thank you
30th Oct 2019, 5:38 PM
Preet