如何在 Java 中关闭扫描仪?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29865150/
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
How do I close a scanner in java?
提问by William Smith
When ever I run the following code,
当我运行以下代码时,
private static String input(String Message){
Scanner input_scanner = new Scanner(System.in);
System.out.print("\n" + Message);
String string = input_scanner.nextLine();
input_scanner.close();
return string;
}
I get this error:
我收到此错误:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at main.learn.input(learn.java:25)
at main.learn.main(learn.java:13)
I figured out it was something to do with the line input_scanner.close();
but when I remove it, I get warnings saying:
我发现这与该行有关,input_scanner.close();
但是当我删除它时,我收到警告说:
Resource leak: "input_scanner" is never closed"
资源泄漏:“input_scanner”永远不会关闭”
Is there anyway I can stop the errors from happening and get rid of the warnings?
无论如何,我可以阻止错误发生并消除警告吗?
采纳答案by SMA
You should check if you have data to consume for Scanner using hasNextLine
api which you could do like:
您应该使用hasNextLine
api检查是否有要为 Scanner 使用的数据,您可以这样做:
String string = "";
if (input_scanner.hasNextLine()) {
string = input_scanner.nextLine();
}
回答by Shondeslitch
Well, first I recommend you to use the API to understand the exceptions that Java shows you.
好吧,首先我建议您使用 API 来了解 Java 向您展示的异常。
Read this for further information about NoSuchElementException:
http://docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html
阅读有关 NoSuchElementException 的更多信息:http:
//docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html
When you use scanner, stringTokenizer, iterators... you have to verify that you have more elements to read before you try read them. If you don't do that, you can get that type of exceptions.
当您使用扫描器、stringTokenizer、迭代器……在尝试阅读它们之前,您必须验证是否有更多元素要阅读。如果你不这样做,你会得到那种类型的异常。
private static String input(String Message){
Scanner input_scanner = new Scanner(System.in);
System.out.print("\n" + Message);
if (input_scanner.hasNextLine()){
String string = input_scanner.nextLine();
}
input_scanner.close();
return string;
If you want to read more than one line, use while (input_scanner.hasNextLine())
如果您想阅读多于一行,请使用 while (input_scanner.hasNextLine())
Your mistake is about the keyboard input. Read this to understand if you are doing well that part: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextLine%28%29
你的错误是关于键盘输入。阅读本文以了解您是否在这部分做得很好:http: //docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextLine%28%29
PS: is recommended to close your scanner with input_scanner.close()
when you have finished with it.
PS:建议用input_scanner.close()
完后关闭扫描仪。
回答by Scott Sosna
use try-with-resources to guarantee that your scanner closes regardless if any exceptions occur.
使用 try-with-resources 来保证您的扫描仪关闭,无论是否发生任何异常。
try (Scanner input_scanner = new Scanner(System.in)) {
.
.
.
}
You can use this with anything that implements AutoCloseable, and it's much better than requiring a bunch of try-catch for exceptions thrown when closing.
您可以将它与任何实现 AutoCloseable 的东西一起使用,这比关闭时抛出的异常需要一堆 try-catch 要好得多。
回答by Nicolas El Khoury
Actually, since you are using Scanner for System.in, this means it is a blocking call that will hold until it receives the first input from the user. Being a local variable, you don't really need to close the scanner, and you definitely don't need a while loop for hasNextLine();
实际上,由于您使用的是 System.in 的 Scanner,这意味着它是一个阻塞调用,它将一直保持到它收到用户的第一个输入。作为一个局部变量,你真的不需要关闭扫描器,而且你绝对不需要 hasNextLine(); 的 while 循环;
private static String input(String Message){
Scanner input_scanner = new Scanner(System.in);
System.out.print("\n" + Message);
String string = input_scanner.nextLine();
return string;
回答by Mohan Raj
A scanning operation may block waiting for input. If no inputs are received then the scanner object does not having to read which makes the Exception to thrown since it does not find the element that need to be close. By verifying does the stream consist anything that could be read by scanning object will helps you in getting this kind of exception.
扫描操作可能会阻塞等待输入。如果没有收到输入,则扫描仪对象不必读取,这会导致抛出异常,因为它没有找到需要关闭的元素。通过验证流是否包含任何可以通过扫描对象读取的内容,这将有助于您获得此类异常。