+ 11

Why we can't use input.nextLine() after input.nextInt() in java??

While using these together second input is skipped

20th Oct 2018, 2:54 AM
Devender Kumar
Devender Kumar - avatar
2 Respostas
+ 5
The first poster did well to explain, so here are some solutions: 1. Use Integer.parseInt(scanner.nextLine()); instead of scanner.nextInt(). This one is more immediately understandable compared to method 2. 2. Add a nextLine() somewhere in between the nextInt() call and the nextLine() call. The newline will now be consumed.
20th Oct 2018, 12:46 PM
LunarCoffee
LunarCoffee - avatar
+ 1
idk how to explain this properly but, when you entering the input for the nextInt the newline are actually registered but not taken by nextInt, you can say its stay in buffer(cmiiw) meanwhile nextLine are wait for a newline before it taking the input, but newline are already in buffer thats why its skipped. its not actually skipped its just think that its already reach delimiter for the input.
20th Oct 2018, 3:41 AM
Taste
Taste - avatar