This is a pretty common problem with the Scanner. When you do the console input, you press the enter key, effectively adding a new line to the input. When you first read the number from the input you do nextInt(), which reads the number but not the new-line. You then ask for the nextLine(), and since there is a new-line waiting in the input stream it immediately returns (with an empty String). You next ask for a nextInt() which lets you input a number, after which you again press the enter key, which enters a new-line after the number, and again the number is consumed by nextInt() but the new-line is not so the process occurs over and over again.
What you need to do is add an additional nextLine() after each nextInt().
Joined: Sep 02, 2012
thanks very much this solve the problem
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: the weirdest problem i came across in java about scanner string