2

Evening all. I am a complete beginner to programming with Java, and I am learning about "Scanner", but when I type this basic code into Eclipse, I get a message saying "Resource leak:'scanner' is never closed.

What am I doing wrong?

package inputting;

import java.util.Scanner;

public class Input {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(scanner.nextLine());
    }
}
Dan
  • 2,701
  • 1
  • 29
  • 34
yxak
  • 23
  • 5

4 Answers4

4

After finishing using the scanner, you must close with the close method:

scanner.close();

The reason why you must close it is because the Scanner class implements the Closeable interface. Straight from the API:

A Closeable is a source or destination of data that can be closed. The close method is invoked to release resources that the object is holding (such as open files).

Essentially, if you never close the Scanner, then the program will continue to seek for input and keep hold of resources. Here is a really simple example:

    Scanner scanner = null;

    try {
        scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            System.out.println(scanner.next());
            //do whatever you need here
        }
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }

Read more about Scanner from the API.

2

Because you are nowhere closing your Scanner. This is problem (classes that deal with I/O should be closed after you're done). You need to close it:

scanner.close();

So a whole code can looks like:

Scanner scanner = new Scanner(System.in);
try {
   // work
}
finally {
   if (scanner != null) {
      scanner.close();
   }
}
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
2

This question is some months old but I would like to add: there is no reason to close System.in (which closing the Scanner will do) and you probably shouldn't. This is an automated warning Eclipse is giving related to Closeable and AutoCloseable and can be ignored if the underlying resource is System.in, System.out or System.err.

For any other resources, the 'correct' idiom has been shown (to ensure the resource gets closed even if exceptions are thrown).

Pre Java 7:

Scanner scanner = null;
try {
    scanner = new Scanner(new FileInputStream("some/file"));

    // statements that throw exceptions
} finally {
    if(scanner != null)
        scanner.close(); // also closes the FileInputStream
}

And post Java 7:

try(Scanner scanner = new Scanner(new FileInputStream("some/file"))) {

    // statements that throw exceptions
}

See also "Why don't we close System.out Stream after using it?" The same is true for System.in: you shouldn't close a resource you did not open and the VM will handle it when the process terminates.

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

You have used scanner for getting a input. After using this we should terminate this by:

input_name.close();

For example in your program, use:

scanner.close();
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63