Java Scanner 不会“完成”读取输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9816965/
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
Java Scanner won't "finish" reading input
提问by mPierce
I've been having trouble using java's Scanner class. I can get it to read my input just fine, but the problem is when I want to output something. Given multiple lines of input, I want to print just ONE line when all the input has been read completely. Here's the code I use for reading input:
我在使用 java 的 Scanner 类时遇到了问题。我可以让它很好地读取我的输入,但问题是当我想输出一些东西时。给定多行输入,当所有输入都被完全读取时,我只想打印一行。这是我用于读取输入的代码:
public static void main(String[] args){
Scanner scanner = new Scanner(System.in); //scanner reads block of input
while(scanner.hasNextLine()){
//body of loop goes here
String s = scanner.nextLine();
Scanner ls = new Scanner(s); //scanner to parse a line of input
while(ls.hasNext()){
//body of nested loop goes here
ls.next();
}
}
System.out.println("Fin");
}
Even when all lines of input have been read, the program doesn't reach the System.out.println message. (Note that the message can't go anywhere else or it will output as many times as the loop is run). How do I fix this? Any help would be greatly appreciated.
即使读取了所有输入行,程序也不会到达 System.out.println 消息。(请注意,该消息不能传到其他任何地方,否则它将输出与运行循环一样多的次数)。我该如何解决?任何帮助将不胜感激。
回答by Shashank Kadne
You are reading from an Infinite stream in this case. hasNextLine()
will keep returning true if there is another line in the input of this scanner. As its a System.in
, it will keep reading from the Keyboard, unless you terminate it or tell the stream to stop.
在这种情况下,您正在从无限流中读取数据。hasNextLine()
如果此扫描仪的输入中有另一行,则将继续返回 true。作为 a System.in
,它将继续从键盘读取,除非您终止它或告诉流停止。
Press "ctrl+Z" in the end, you will see that it works.
最后按“ctrl+Z”,你会看到它起作用了。
Edit: You could do something like this...
编辑:你可以做这样的事情......
Scanner scanner = new Scanner(System.in); //scanner reads block of input
int BLOCK_SIZE =3,count=1;
while(scanner.hasNextLine()){
//body of loop goes here
String s = scanner.nextLine();
Scanner ls = new Scanner(s); //scanner to parse a line of input
while(ls.hasNext()){
//body of nested loop goes here
ls.next();
}
if(count++==BLOCK_SIZE)
{
break;
}
}
System.out.println("Fin");
}
回答by me_digvijay
As I can see in your outer while loop you have used
正如我在你使用的外部 while 循环中看到的
scanner.hasNextLine();
method. This method gets blocked if it has to wait for the input. Also you have
方法。如果必须等待输入,则此方法将被阻止。你还有
Scanner scanner = new Scanner(System.in);
statement. So the system.in will be waiting for input all the time, hence the hasNextLine() method has to wait for the input. That is why the control gets stuck in the loop and can't proceed further.
陈述。因此 system.in 将一直等待输入,因此 hasNextLine() 方法必须等待输入。这就是为什么控件卡在循环中并且无法继续进行的原因。
To fix it you can first store input in a string variable and the call the scanner constructor on it.
要修复它,您可以先将输入存储在字符串变量中,然后在其上调用扫描仪构造函数。
回答by Dunes
You need to tell the program that there is going to be no more input. This is done by appending an EOF character. This can be done manually on Linux by pressing Ctrl-D in the console. I think on Windows you can press Ctrl-Z. The stream will be automatically closed if you are piping input from one program to another.
您需要告诉程序将不再有输入。这是通过附加一个 EOF 字符来完成的。这可以通过在控制台中按 Ctrl-D 在 Linux 上手动完成。我认为在 Windows 上你可以按 Ctrl-Z。如果您将输入从一个程序传送到另一个程序,该流将自动关闭。
eg.
例如。
cat filename | java MyJavaProgram
回答by Mirror318
The magic of
的魔力
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
Is that there will never stop being input from System (unless you close the input with ctrl+d (for macs)).
是否永远不会停止从系统输入(除非您使用 ctrl+d 关闭输入(对于 macs))。
To stop the loop, I suggest throw something more in the condition than just hasNextLine().
为了停止循环,我建议在条件中抛出更多的东西,而不仅仅是 hasNextLine()。
E.g.
例如
Scanner scanner = new Scanner(System.in); //scanner reads block of input
int BLOCK_SIZE =3,count=1;
while(scanner.hasNextLine() && count++ <= BLOCK_SIZE){ //<- Use count here.
//body of loop goes here
String s = scanner.nextLine();
Scanner ls = new Scanner(s); //scanner to parse a line of input
while(ls.hasNext()){
//body of nested loop goes here
ls.next();
}
}
System.out.println("Fin");
}