I am using the Scanner
methods nextInt()
and nextLine()
for reading input.
It looks like this:
System.out.println("Enter numerical value");
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string");
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
The problem is that after entering the numerical value, the first input.nextLine()
is skipped and the second input.nextLine()
is executed, so that my output looks like this:
Enter numerical value
3 // This is my input
Enter 1st string // The program is supposed to stop here and wait for my input, but is skipped
Enter 2nd string // ...and this line is executed and waits for my input
I tested my application and it looks like the problem lies in using input.nextInt()
. If I delete it, then both string1 = input.nextLine()
and string2 = input.nextLine()
are executed as I want them to be.
If you want to scan input fast without getting confused into Scanner class nextLine() method , Use Custom Input Scanner for it .
Code :
Advantages :
Methods :
Usage :
ScanReader sc = new ScanReader(System.in);
3. Import necessary Classes :import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream;
4. Throw IOException from your main method to handle Exception 5. Use Provided Methods. 6. EnjoyExample :
Use 2 scanner objects instead of one
if I expect a non-empty input
used in above example:
I guess I'm pretty late to the party..
As previously stated, calling
input.nextLine()
after getting your int value will solve your problem. The reason why your code didn't work was because there was nothing else to store from your input (where you inputted the int) intostring1
. I'll just shed a little more light to the entire topic.Consider nextLine() as the odd one out among the nextFoo() methods in the Scanner class. Let's take a quick example.. Let's say we have two lines of code like the ones below:
If we input the value below (as a single line of input)
The value of our
firstNumber
andsecondNumber
variable become 54 and 234 respectively. The reason why this works this way is because a new line feed (i.e \n) IS NOT automatically generated when the nextInt() method takes in the values. It simply takes the "next int" and moves on. This is the same for the rest of the nextFoo() methods except nextLine().nextLine() generates a new line feed immediately after taking a value; this is what @RohitJain means by saying the new line feed is "consumed".
Lastly, the next() method simply takes the nearest String without generating a new line; this makes this the preferential method for taking separate Strings within the same single line.
I hope this helps.. Merry coding!
Use this code it will fix your problem.
That's because the
Scanner.nextInt
method does not read the newline character in your input created by hitting "Enter," and so the call toScanner.nextLine
returns after reading that newline.You will encounter the similar behaviour when you use
Scanner.nextLine
afterScanner.next()
or anyScanner.nextFoo
method (exceptnextLine
itself).Workaround:
Either put a
Scanner.nextLine
call after eachScanner.nextInt
orScanner.nextFoo
to consume rest of that line including newlineOr, even better, read the input through
Scanner.nextLine
and convert your input to the proper format you need. For example, you may convert to an integer usingInteger.parseInt(String)
method.