Resource leak: 'in' is never closed

user1686995 picture user1686995 · Sep 20, 2012 · Viewed 341.1k times · Source

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

public void readShapeData() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the width of the Rectangle: ");
        width = in.nextDouble();
        System.out.println("Enter the height of the Rectangle: ");
        height = in.nextDouble();

Answer

nogard picture nogard · Sep 20, 2012

Because you don't close your Scanner

in.close();