score:-1

in.close();
scannerobject.close(); 

it will close scanner and shut the warning.

score:0

scanner sc = new scanner(system.in);

//do stuff with sc

sc.close();//write at end of code.

score:1

the scanner should be closed. it is a good practice to close readers, streams...and this kind of objects to free up resources and aovid memory leaks; and doing so in a finally block to make sure that they are closed up even if an exception occurs while handling those objects.

score:1

private static scanner in;

i fixed it by declaring in as a private static scanner class variable. not sure why that fixed it but that is what eclipse recommended i do.

score:2

generally, instances of classes that deal with i/o should be closed after you're finished with them. so at the end of your code you could add in.close().

score:3

you should close your scanner when you're done with it:

in.close();

score:5

adding private static scanner in; does not really fix the problem, it only clears out the warning. making the scanner static means it remains open forever (or until the class get's unloaded, which nearly is "forever"). the compiler gives you no warning any more, since you told him "keep it open forever". but that is not what you really wanted to, since you should close resources as soon as you don't need them any more.

hth, manfred.

score:5

okay, seriously, in many cases at least, this is actually a bug. it shows up in vs code as well, and it's the linter noticing that you've reached the end of the enclosing scope without closing the scanner object, but not recognizing that closing all open file descriptors is part of process termination. there's no resource leak because the resources are all cleaned up at termination, and the process goes away, leaving nowhere for the resource to be held.

score:6

if you are using jdk7 or 8, you can use try-catch with resources.this will automatically close the scanner.

try ( scanner scanner = new scanner(system.in); )
  {
    system.out.println("enter the width of the rectangle: ");
    width = scanner.nextdouble();
    system.out.println("enter the height of the rectangle: ");
    height = scanner.nextdouble();
  }
catch(exception ex)
{
    //exception handling...do something (e.g., print the error message)
    ex.printstacktrace();
}

score:7

it is telling you that you need to close the scanner you instantiated on system.in with scanner.close(). normally every reader should be closed.

note that if you close system.in, you won't be able to read from it again. you may also take a look at the console class.

public void readshapedata() {
    console console = system.console();
    double width = double.parsedouble(console.readline("enter the width of the rectangle: "));
    double height = double.parsedouble(console.readline("enter the height of the rectangle: "));
    ...
}

score:7

// an inputstream which is typically connected to keyboard input of console programs

scanner in= new scanner(system.in);

above line will invoke constructor of scanner class with argument system.in, and will return a reference to newly constructed object.

it is connected to a input stream that is connected to keyboard, so now at run-time you can take user input to do required operation.

//write piece of code 

to remove the memory leak -

in.close();//write at end of code.

score:12

you need call in.close(), in a finally block to ensure it occurs.

from the eclipse documentation, here is why it flags this particular problem (emphasis mine):

classes implementing the interface java.io.closeable (since jdk 1.5) and java.lang.autocloseable (since jdk 1.7) are considered to represent external resources, which should be closed using method close(), when they are no longer needed.

the eclipse java compiler is able to analyze whether code using such types adheres to this policy.

...

the compiler will flag [violations] with "resource leak: 'stream' is never closed".

full explanation here.

score:69

as others have said, you need to call 'close' on io classes. i'll add that this is an excellent spot to use the try - finally block with no catch, like this:

public void readshapedata() throws ioexception {
    scanner in = new scanner(system.in);
    try {
        system.out.println("enter the width of the rectangle: ");
        width = in.nextdouble();
        system.out.println("enter the height of the rectangle: ");
        height = in.nextdouble();
    } finally {
        in.close();
    }
}

this ensures that your scanner is always closed, guaranteeing proper resource cleanup.

equivalently, in java 7 or greater, you can use the "try-with-resources" syntax:

try (scanner in = new scanner(system.in)) {
    ... 
}

score:90

because you don't close your scanner

in.close();

Related Query

More Query from same tag