0

Why my nextLine() doesnt work?

In my code I scan an int, then this String and then another int. But it skips the scan of the String. I looked for it in Stackoverflow and its a common error with two solutions: to write another nextLine() before or to use try and catch (I don't know how does it work yet). I use the first solution in NetBeans, in my computer and it worked but doesn't work here. What do you reccomend?

13th Jun 2017, 4:26 AM
Daniel Leonard
Daniel Leonard - avatar
2 Réponses
+ 5
Great question! I spent more than 6 months to get this question's answer. It's because when you enter a number then press Enter, input.nextInt() consumes only the number, not the "end of line". When input.nextLine() executes, it consumes the "end of line" still in the buffer from the first input. Instead, use input.nextLine() immediately after input.nextInt() Here is the source- https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo
13th Jun 2017, 4:47 AM
Sachin Artani
Sachin Artani - avatar
0
If your input will be as follows: 1. 45 2. 45.6 3. Styd dufigk ufugigj fugjv Three input !! Then how you take from user as my nextLine() not work for last line if I try to take by nextLine() it gives me "styd" only
27th Dec 2019, 6:18 PM
AARTI SHELAR
AARTI SHELAR - avatar