javaeclipsejava.util.scannerinstance-variablesresource-leak

Leak instance of Scanner class in Java using Eclipse


I am new to Java. Can anyone tell me how to remove this resource leak error? The error says the resource leak is never closed.

resource leak


Solution

  • If you are using Java 7+ version you can use try with resources like the below.

        public static void main(String args[]) {
            try (Scanner in = new Scanner(System.in)) {
                System.out.println("Enter a number - ");
                int n = in.nextInt();
                System.out.println("N is " + n);
            }
        }