For this line of my Java code:
Scanner s = new Scanner(System.in);
This occurs:
Resource leak: 's' is never closed
What does it mean and why is it occurring?
For this line of my Java code:
Scanner s = new Scanner(System.in);
This occurs:
Resource leak: 's' is never closed
What does it mean and why is it occurring?
Short answer: Ignore it.
Explanation:
This is a warning stating that the Scanner (s) is never closed.
Usually you want to close scanners when you are done with them in order to allow Java to reclaim the memory that the scanner has allocated to temporarily store the data from the InputStream. Avoiding to do so causes a memory(/resource) leak, meaning the memory is claimed permanently.
However in this case, the InputStream is the Systems input (System.in). You do not have to close the Scanner in that case (and propably shouldn't since doing so would also close the Systems input Stream for good). System.in is automatically closed when your Java program terminates, so there is no leak. You should simply ignore it as it doesn't affect anything at all, it's just a warning.
My guess is that you are writing Java in Visual Studio Code. In other IDEs (Netbeans for example) this warning doesn't even occur when using Scanner with System.in. My guess could be wrong tho.
(If I somehow explain something wrong or missed something, someone please respectfully correct me in the comments. Thank you in advance.)
Use try-catch and close your scanner :
try{
// your code
s.close();
}catch (Exception e){
System.out.println("Error : " + e.getMessage());
}