Scanner method | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок

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
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 - avatar
+ 1
Now the programm works without any problem. Thank you
30th Oct 2019, 5:38 PM