'Resource leak: 'sc' is never closed in vs code (I'm using the java coding pack from vs code website) [duplicate]
Why is vs code giving me a warning on this: (The warning is below the code)
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String input = sc.nextLine();
System.out.println(input);
}
}
The warning is:
Resource leak: 'sc' is never closed
Solution 1:[1]
You can simply put sc.close() and this error will no more be displayed. It is not a compulsion. Without this statement also program works fine but if your eyes are getting disturbed by again and again popping u of the message of error then you can put sc.close(). Note that after you put it, you can not use sc to get input after that statement.
To sort this error:
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String input = sc.nextLine();
System.out.println(input);
sc.close();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Utkarsh Sahu |
