+ 11
Why we can't use input.nextLine() after input.nextInt() in java??
While using these together second input is skipped
2 odpowiedzi
+ 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.
+ 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.