java 为什么 nextLine() 返回一个空字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16040601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:42:48  来源:igfitidea点击:

Why is nextLine() returning an empty string?

java

提问by Favolas

This is probably one of the easiest things but I'm not seeing what I'm doing wrong.

这可能是最简单的事情之一,但我没有看到我做错了什么。

My input consist of one first line with a number (the number of lines to read), a bunch of lines with data and a final line only with \n. I should process this input and after the last line, do some work.

我的输入包括一个带有数字的第一行(要读取的行数)、一堆带有数据的行和最后一行只有 \n。我应该处理这个输入,在最后一行之后,做一些工作。

I have this input:

我有这个输入:

5
test1
test2
test3
test4
test5
      /*this is a \n*/

And for reading the input I have this code.

为了读取输入,我有这个代码。

int numberRegisters;
String line;

Scanner readInput = new Scanner(System.in);

numberRegisters = readInput.nextInt();

while (!(line = readInput.nextLine()).isEmpty()) {
    System.out.println(line + "<");
}

My question is why I'm not printing anything? Program reads the first line and then does nothing.

我的问题是为什么我不打印任何东西?程序读取第一行,然后什么都不做。

回答by Bernhard Barker

nextIntdoesn't read the following new-line character, so the first nextLine(which returns the rest of the currentline) will always return an empty string.

nextInt不读取以下换行符,因此第一个nextLine返回当前行的其余部分)将始终返回空字符串。

This should work:

这应该有效:

numberRegisters = readInput.nextInt();
readInput.nextLine();
while (!(line = readInput.nextLine()).isEmpty()) {
    System.out.println(line + "<");
}

But my advice is not to mix nextLinewith nextInt/ nextDouble/ next/ etc. because anyone trying to maintain the code (yourself included) may not be aware of, or have forgotten, the above, so may be somewhat confused by the above code.

但我的建议是不要nextLinenextInt// nextDouble/next等混用,因为任何试图维护代码的人(包括你自己)可能都没有意识到或忘记了上述内容,因此可能会对上述代码有些困惑。

So I suggest:

所以我建议:

numberRegisters = Integer.parseInt(readInput.nextLine());

while (!(line = readInput.nextLine()).isEmpty()) {
    System.out.println(line + "<");
}

回答by Sam I am says Reinstate Monica

I think I've see this issue before. I think you need to add another readInput.nextLine()or else you're just reading between the end of the 5, and the \nafter that

我想我以前见过这个问题。我认为您需要添加另一个readInput.nextLine(),否则您只是在 的结尾5\n之后阅读

int numberRegisters;
String line;

Scanner readInput = new Scanner(System.in);

numberRegisters = readInput.nextInt();
readInput.nextLine();

while (!(line = readInput.nextLine()).isEmpty()) {
    System.out.println(line + "<");
}

回答by RiaD

Actually it doesn't answer the question completely(why your code isn't working) but you may use following code.

实际上它并没有完全回答这个问题(为什么你的代码不起作用)但你可以使用以下代码。

int n = Integer.parseInt(readInput.readLine());
for(int i = 0; i < n; ++i) {
    String line = readInput().readLine();
    // use line here
}

As for me it's more readable and even may save your time in such rare cases when testcases are incorrect(with extra info at the end of file)

对于我来说,它更具可读性,甚至可以在测试用例不正确的极少数情况下节省您的时间(文件末尾有额外信息)

BTW, seems you take part in some programming competition. Make note, that Scanner may be quite slow to input a lot of data. you may consider using BufferedReaderwith possible StringTokenizer(not needed in this task)

顺便说一句,您似乎参加了一些编程比赛。请注意,扫描仪输入大量数据可能会很慢。您可以考虑使用BufferedReaderwith possible StringTokenizer(在此任务中不需要)