• Post Reply Bookmark Topic Watch Topic
  • New Topic

Resource leak warning from Scanner class.  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the appropriate place to close the scanner on this method. i've tried closing it on different places but every time i run the program , it crashes.
The method is being called on a switch statement inside of a while loop. i know i can close the scanner calling the method close.

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting a warning from your IDE about a resource leak?  For Scanner opened on System.in only, you can ignore that warning.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the IDE is reporting this warning. Alright, i was just trying to get rid of the underlined yellow line, its bothering me. 
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick wrote:What is the appropriate place to close the scanner on this method. i've tried closing it on different places but every time i run the program , it crashes.
Hello rick,
I will recommend you to not close the scanner(specially when the standard input stream is wrapped with it).In your case you are reading from a standard input and if you close the scanner it will also close the wrapped stream,here→*System.in* which means once a scanner is closed you would not be able to read again from the standard input.suppose you are required to invoke this method twice then think about the outcome.

if you still required to close it then you can use a try finally block or try with resources block as System.in implements a AutoCloseable interface.

Hopeit helps!

Kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: this is Eclipse overreacting. But I think you shou‍ld be more emphatic. You need to be aware of this warning and you need to know that in the case of System.in you must ignore it. There may be an option in Eclipse to sort it out with the @SuppressWarnings annotation; click on the yellow mark to find out. It will give you a dropdown list of options and that will probably include @SuppressWarnings.
One advantage of using Eclipse is that it sometimes gives better error messages than the Oracle compiler, but it is capable of overreacting. You will probably get the same sort of warning, which you must also ignore, if you write something like this:-
Formatter outputFormatter = new Formatter(System.out);
The same applies to System.err.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried my suggestion on Eclipse and that is exactly what happened: I now have code like this.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!