扫描仪 - java.util.NoSuchElementException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20739587/
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
Scanner - java.util.NoSuchElementException
提问by Alan
Does anyone see a problem with this? First input works fine, but after the first loop, it doesn't ask to enter a value again. How do I fix this?
有没有人看到这个问题?第一次输入工作正常,但在第一次循环之后,它不会要求再次输入值。我该如何解决?
int value;
while(true)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a value");
try
{
value = scan.nextInt();
System.out.println("value provided is: " + value);
scan.nextLine(); // consumes "\n" character in buffer
}
catch(InputMismatchException e) // outputs error message if value provided is not an integer
{
System.out.println("Incorrect input type. Try again.");
continue; // restarts while loop to allow for re-entering of a valid input
}
scan.close();
}
采纳答案by Maroun
Move scan.close();
to outside the while
loop.
移动scan.close();
到外while
循环。
Also you don't have to construct a newScanner
on each iteration. Move the declaration to outside the loop as well.
此外,您不必在每次迭代中构建一个新的Scanner
。也将声明移到循环之外。
When close
the Scanner, this closes the System.in
input stream.
So now when you try to instantiate it again, it doesn't find any opened stream and you'll get that exception.
所以现在当您尝试再次实例化它时,它找不到任何打开的流,您将收到该异常。
回答by Adarsh
At the end of the while loop you have written scan.close()
. This will close the scanner preventing any further scans. Removing that statement would ensure your while loop keeps asking you for the number(will be an infinite loop in your case)
在 while 循环结束时,您编写了scan.close()
. 这将关闭扫描仪以防止任何进一步的扫描。删除该语句将确保您的 while 循环不断询问您的数字(在您的情况下将是一个无限循环)
Also, scan.nextInt()
in effect ignores all new line and waits till you actually input a number and hit enter. So, scan.nextLine()
can be omitted. You need that only in case where you use scan.nextLine()
to fetch the value entered. In that case, the new line character is also read as an input, as a result of which you need an extra nextLine() call to consume it.
此外,scan.nextInt()
实际上会忽略所有新行并等待您实际输入数字并按回车键。所以,scan.nextLine()
可以省略。只有在您scan.nextLine()
用来获取输入的值的情况下才需要它。在这种情况下,新行字符也被读取为输入,因此您需要额外的 nextLine() 调用来使用它。
回答by tuxdna
When you do scan.close()
, it closes the underlying System.in
stream. So in the next iteration it will not have anything to read.
当你这样做时scan.close()
,它会关闭底层System.in
流。因此,在下一次迭代中,它将没有任何可读取的内容。
For example:
例如:
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int value;
while (true) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a value");
try {
value = scan.nextInt();
System.out.println("value provided is: " + value);
scan.nextLine(); // consumes "\n" character in buffer
} catch (InputMismatchException e) // outputs error message if value
// provided is not an integer
{
System.out.println("Incorrect input type. Try again.");
continue; // restarts while loop to allow for re-entering of a
// valid input
}
scan.close();
try {
int x = System.in.read();
System.out.println(x);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
OUTPUT:
输出:
Enter a value
10
value provided is: 10
Enter a value
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at Main.main(Main.java:24)
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Main.main(Main.java:12)
Check this question for more:
检查这个问题了解更多:
Is it safe not to close a Java Scanner, provided I close the underlying readable?