ARTICLE AD BOX
The warning is technically correct. Scanner implements Closeable, so static analyzers warn if you never close it.
Your code works because the JVM exits immediately after main() finishes, but the IDE still flags it as a potential resource leak.
The proper fix is:
import java.util.*; public class TakingInput { public static void main(String args[]) { try (Scanner sc = new Scanner(System.in)) { float price = sc.nextFloat(); System.out.println(price); } } }This is called try-with-resources. Java automatically closes sc.
One important detail:
sc.close();also closes System.in.
So in small beginner programs this is fine, but in larger applications you sometimes intentionally avoid closing a Scanner created from System.in because it would make further console input impossible.
That’s why some developers simply ignore this warning for tiny console apps. But the cleanest modern Java approach is the try (...) version above.
