How to close a scanner in java?

When 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 am getting 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 it had to do with the line input_scanner.close();

, but when I remove it I get warnings:

Resource leak: "input_scanner" never closes "

Is there anyway I can stop the errors and get rid of the warnings?

+3


source to share


5 answers


You should check if you have data to use in Scanner using hasNextLine

api, which you could do like this:



String string = "";
if (input_scanner.hasNextLine()) {
    string = input_scanner.nextLine();
}

      

+3


source


Ok, first I recommend that you use the API to understand the exceptions that Java is showing you.

Read this for more information on NoSuchElementException:
http://docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html

When you use a scanner, stringTokenizer, iterators ... you have to make sure you have more items to read before you try to read them. If you don't, you may receive such 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 then use while (input_scanner.hasNextLine())

Your mistake is that you are using the keyboard. Read this to understand if you succeed in this part: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextLine%28%29

PS: It is recommended that you close your scanner with input_scanner.close()

when you are done with it.

+8


source


use try-with-resources to ensure that your scanner closes regardless of whether any exceptions occur.

try (Scanner input_scanner = new Scanner(System.in)) {
   .
   .
   .
}

      

You can use this with anything that implements AutoCloseable and it is much better than requiring a bunch of try-catch for exceptions thrown on close.

+2


source


In fact, since you are using Scanner for System.in, this means that this is a blocking call that will be held until it receives the first input from the user. As a local variable, you don't really need to close the scanner, and you definitely don't need a while loop for hasNextLine ();

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;

      

+1


source


The scan operation may block waiting for input. If no input is received, then the scanner object should not read, which makes an exception because it does not find an element that should be close. Checking if the stream contains everything that can be read by the scan target will help you in getting such an exception.

+1


source







All Articles