简体   繁体   English

如何在java中关闭扫描仪?

[英]How do I close a scanner in java?

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(); 我发现这与行input_scanner.close(); but when I remove it, I get warnings saying: 但当我删除它时,我收到警告:

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? 无论如何,我可以阻止错误发生并摆脱警告?

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: 有关NoSuchElementException的更多信息,请阅读此内容:
http://docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html 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. 当您使用scanner,stringTokenizer,迭代器......您必须在尝试阅读之前验证是否有更多要阅读的元素。 If you don't do that, you can get that type of exceptions. 如果您不这样做,则可以获得该类型的异常。

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()关闭扫描仪。

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();
}

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要好得多。

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. 实际上,由于您使用的是Scanner for System.in,这意味着它是一个阻塞调用,直到它收到用户的第一个输入为止。 Being a local variable, you don't really need to close the scanner, and you definitely don't need a while loop for hasNextLine(); 作为一个局部变量,你真的不需要关闭扫描器,你肯定不需要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;

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. 如果没有接收到输入,那么scanner对象不必读取哪个使得抛出异常,因为它没有找到需要关闭的元素。 By verifying does the stream consist anything that could be read by scanning object will helps you in getting this kind of exception. 通过验证流是否包含扫描对象可以读取的任何内容将帮助您获得此类异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM