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.
TL;DR Use
scanner.skip("\\R")
before eachscanner.newLine()
call, which is executed after:scanner.next()
scanner.next*TYPE*()
method.Things you need to know:
text which represents few lines also contains non-printable characters between lines (we call them line separators) like
"\r"
)"\n"
)when you are reading data from the console, it allows the user to type his response and when he is done he needs to somehow confirm that fact. To do so, the user is required to press "enter"/"return" key on the keyboard.
What is important is that this key beside ensuring placing user data to standard input (represented by
System.in
which is read byScanner
) also sends OS dependant line separators (like for Windows\r\n
) after it.So when you are asking the user for value like
age
, and user types 42 and presses enter, standard input will contain"42\r\n"
.Problem
Scanner#nextInt
(and otherScanner#nextType
methods) doesn't allow Scanner to consume these line separators. It will read them fromSystem.in
(how else Scanner would know that there are no more digits from the user which representage
value than facing whitespace?) which will remove them from standard input, but it will also cache those line separators internally. What we need to remember, is that all of the Scanner methods are always scanning starting from the cached text.Now
Scanner#nextLine()
simply collects and returns all characters until it finds line separators (or end of stream). But since line separators after reading the number from the console are found immediately in Scanner's cache, it returns empty String, meaning that Scanner was not able to find any character before those line separators (or end of stream).BTW
nextLine
also consumes those line separators.Solution
So when you want to ask for number and then for entire line while avoiding that empty string as result of
nextLine
, eithernextInt
from Scanners cache bynextLine
,skip("\\R")
orskip("\r\n|\r|\n")
to let Scanner skip part matched by line separator (more info about\R
: https://stackoverflow.com/a/31060125)nextInt
(nornext
, or anynextTYPE
methods) at all. Instead read entire data line-by-line usingnextLine
and parse numbers from each line (assuming one line contains only one number) to proper type likeint
viaInteger.parseInt
.BTW:
Scanner#nextType
methods can skip delimiters (by default all whitespaces like tabs, line separators) including those cached by scanner, until they will find next non-delimiter value (token). Thanks to that for input like"42\r\n\r\n321\r\n\r\n\r\nfoobar"
codewill be able to properly assign
num1=42
num2=321
name=foobar
.There seem to be many questions about this issue with
java.util.Scanner
. I think a more readable/idiomatic solution would be to callscanner.skip("[\r\n]+")
to drop any newline characters after callingnextInt()
.EDIT: as @PatrickParker noted below, this will cause an infinite loop if user inputs any whitespace after the number. See their answer for a better pattern to use with skip: https://stackoverflow.com/a/42471816/143585
In order to avoid the issue, use
nextLine();
immediately afternextInt();
as it helps in clearing out the buffer. When you pressENTER
thenextInt();
does not capture the new line and hence, skips theScanner
code later.Why not use a new Scanner for every reading? Like below. With this approach you will not confront your problem.
In one of my usecase, I had the scenario of reading a string value preceded by a couple of integer values. I had to use a "for / while loop" to read the values. And none of the above suggestions worked in this case.
Using
input.next()
instead ofinput.nextLine()
fixed the issue. Hope this might be helpful for those dealing with similar scenario.