本文翻译自:Scanner is skipping nextLine() after using next() or nextFoo()?
I am using the Scanner
methods nextInt()
and nextLine()
for reading input. 我正在使用Scanner
方法nextInt()
和nextLine()
读取输入。
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: 问题是输入数值后,第一个input.nextLine()
被跳过,第二个input.nextLine()
被执行,因此我的输出看起来像这样:
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()
. 我测试了我的应用程序,看起来问题出在使用input.nextInt()
。 If I delete it, then both string1 = input.nextLine()
and string2 = input.nextLine()
are executed as I want them to be. 如果我删除它,那么string1 = input.nextLine()
和string2 = input.nextLine()
都按照我希望的方式执行。
#1楼
参考:https://stackoom.com/question/syRJ/在使用next-或nextFoo-之后-扫描仪正在跳过nextLine-吗
#2楼
That's because the Scanner.nextInt
method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine
returns after reading that newline . 这是因为Scanner.nextInt
方法不会在您按“ Enter”键创建的输入中读取换行符 ,因此对Scanner.nextLine
的调用在读取该换行符后返回。
You will encounter the similar behaviour when you use Scanner.nextLine
after Scanner.next()
or any Scanner.nextFoo
method (except nextLine
itself). 在Scanner.next()
或任何Scanner.nextFoo
方法( nextLine
本身除外Scanner.next()
之后使用Scanner.nextLine
时,会遇到类似的行为。
Workaround: 解决方法:
Either put a
Scanner.nextLine
call after eachScanner.nextInt
orScanner.nextFoo
to consume rest of that line including newline 在每个Scanner.nextInt
或Scanner.nextFoo
之后放置Scanner.nextLine
调用,以消耗该行的其余部分,包括换行符int option = input.nextInt(); input.nextLine(); // Consume newline left-over String str1 = input.nextLine();
Or, even better, read the input through
Scanner.nextLine
and convert your input to the proper format you need. 或者,甚至更好的是,通过Scanner.nextLine
读取输入,并将输入转换为所需的正确格式。 For example, you may convert to an integer usingInteger.parseInt(String)
method. 例如,您可以使用Integer.parseInt(String)
方法转换为整数。int option = 0; try { option = Integer.parseInt(input.nextLine()); } catch (NumberFormatException e) { e.printStackTrace(); } String str1 = input.nextLine();
#3楼
It does that because input.nextInt();
这样做是因为input.nextInt();
doesn't capture the newline. 不捕获换行符。 you could do like the others proposed by adding an input.nextLine();
您可以通过添加input.nextLine();
像其他建议的那样做input.nextLine();
underneath. 下。
Alternatively you can do it C# style and parse a nextLine to an integer like so: 另外,您也可以使用C#样式,将nextLine解析为整数,如下所示:
int number = Integer.parseInt(input.nextLine());
Doing this works just as well, and it saves you a line of code. 这样做同样有效,并且可以节省一行代码。
#4楼
There seem to be many questions about this issue with java.util.Scanner
. 关于java.util.Scanner
问题似乎有很多问题。 I think a more readable/idiomatic solution would be to call scanner.skip("[\\r\\n]+")
to drop any newline characters after calling nextInt()
. 我认为一种更具可读性/惯用性的解决方案是,在调用nextInt()
之后,调用nextInt()
scanner.skip("[\\r\\n]+")
删除所有换行符。
EDIT: as @PatrickParker noted below, this will cause an infinite loop if user inputs any whitespace after the number. 编辑:如下@PatrickParker所述,如果用户在数字后输入任何空格,这将导致无限循环。 See their answer for a better pattern to use with skip: https://stackoverflow.com/a/42471816/143585 请参阅他们的答案以获取与跳过一起使用的更好模式: https : //stackoverflow.com/a/42471816/143585
#5楼
Instead of input.nextLine()
use input.next()
, that should solve the problem. 代替input.nextLine()
使用input.next()
,应该可以解决问题。
Modified code: 修改后的代码:
public static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Insert a number: ");
int number = input.nextInt();
System.out.print("Text1: ");
String text1 = input.next();
System.out.print("Text2: ");
String text2 = input.next();
}
#6楼
Why not use a new Scanner for every reading? 为什么不为每个阅读使用新的扫描仪? Like below. 像下面。 With this approach you will not confront your problem. 使用这种方法,您将不会遇到问题。
int i = new Scanner(System.in).nextInt();