Recently I am learning java programming. The configuration of installation and environment variables will be described in detail in the following blog. This blog mainly introduces the warning of “Resource leak:’sc’ is never closed” in java input.
The test code is as follows. import java.util.*; public class Print { public static void main(String[] args) { double num=0;; Scanner sc=new Scanner(System.in); num = sc.nextDouble(); if(num == 1){ System.out.println("1"); } else{ System.out.println("0"); } } }
This may bring some security problems, so how to solve this problem? Since’sc’ is not closed, then we only need to close’sc’. At this time, we need to add’sc.close();’ to the end to solve it.
The modified code is as follows. import java.util.*; public class Print { public static void main(String[] args) { double num=0;; Scanner sc=new Scanner(System.in); num = sc.nextDouble(); sc.close(); if(num == 1){ System.out.println("1"); } else{ System.out.println("0"); } } }
Similar Posts:
- Error log: resource leak: ‘xxx’ is never closed (precautions for scanner class)
- [Solved] Java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
- How to Solve Error: Scanner class java. Util. NoSuchElementException exception
- Java – Stream has already been operated upon or closed
- Java error: No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- [Two Solutions] non-static method xxx() cannot be referenced from a static context
- Eclipse Error:The selection cannot be launched, and there are no recent launches
- [Problems with the use of internal classes] No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclo
- [Solved] JAVA Beginners’ Error: Exception in thread “main“ java.io.FileNotFoundException
- Convert Object to List>, avoiding Unchecked cast: ‘java.lang.Object’ to ‘java.util.List