0

I am a Java Beginner.

I got an error when running this code.

It says the following error: Resource Leak: 'yourName' is never closed.

And i am using VS Code insiders.

The file name is PrintName.java and the code is -->

import java.util.*;
public class PrintName {
    public static void main(String[] args) {
        // Prints your name
        System.out.print("What is your name? ");
        Scanner yourName = new Scanner(System.in);
        System.out.println("Your name is: " + yourName.next());
    }
}

And.. could anyone help me with the solution please?

  • 2
    Dont close scanners tied to `System.in`. You are not responsible for closing `System.in`, the JVM is. In this case, this is a false-positive warning, suppress it. – Zabuzard Nov 10 '21 at 11:16
  • In all other cases, you should close what you have opened indeed. But `System.in`, `System.out` and `System.err` are special cases. They are I/O streams wired by the JVM at startup. – MC Emperor Nov 10 '21 at 13:31

0 Answers0