0

Why does Eclipse give me the warming "Resource leak: 'in' is never closed" in the following code?

import java.util.*;
 
public class EvenNumberSum{
    public static void main(String args[]) {
        int N, i, sum = 0;
 
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number");
        N = in.nextInt();
 
        for(i = 0; i <= N; i++){
            if((i%2) == 1){
                sum += i;
            }
        }
     
        System.out.print ("Sum of all odd numbers between 0 to " + N + " = " + sum);
        

        in.close();
    }
}

  • 2
    *in.close();* Even with that in it does it give you the message? Incidentally, you don't close `System.in` - it's essentially stdin and lasts as long as the process – g00se Mar 09 '23 at 16:57
  • 1
    Does this answer your question? [What is the reason for this finally clause containing close() calls](https://stackoverflow.com/questions/22609779/what-is-the-reason-for-this-finally-clause-containing-close-calls) – Karl Knechtel Mar 09 '23 at 16:58
  • @user16320675 There's the same warning even if `nextInt` is not called. – Unmitigated Mar 09 '23 at 17:00
  • 1
    You can ignore it - it's wrong for the reason I mentioned. It would be different were it a file – g00se Mar 09 '23 at 17:01

0 Answers0