Java resource leak '<unassigned close value>' will not close.

Asked 1 years ago, Updated 1 years ago, 41 views

public class Practice 7 {
  public static void main(String[]args) {
    int num1 = new java.util.Scanner(System.in).nextInt();
    int num2 = new java.util.Scanner(System.in).nextInt();

    System.out.println(num1+num2);
  }
}

I type num1 and num2 on the keyboard and enter the code for the sum.
Yellow to the left of the two lines int num1=... and int num2=...!Marked and light volves,
If you take the cursor over there, it says, "Resource leak '<Unassigned Closeable Value>' will never close."

I don't know what's wrong and I don't understand this resource... so I'd appreciate it if you could let me know m__m

java

2022-09-30 21:29

1 Answers

I think it's a message to Scanner that the close method is not called.

Can I get that message with the code below?

public static class Practice 7 {
    public static void main(String[]args) {

        Scanner scanner = new java.util.Scanner (System.in);

        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();

        scanner.close();
        System.out.println(num1+num2);
    }
}

Also, Scanner implements AutoClosable, so if it is Java 7 or later, it can be closed using try-with-resource writing.


2022-09-30 21:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.