Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By SurfMan
  • 1 Post By JosAH

Thread: "myScanner" never close. Need Help

  1. #1
    IamShahir is offline Newbie
    Join Date
    Mar 2016
    Location
    Malaysia
    Posts
    3
    Rep Power
    0

    Default "myScanner" never close. Need Help

    I can run the code. It said that Resource leak: 'myScanner' never close. I'm new to Java so I don't get it. Please help me fix it.


  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: "myScanner" never close. Need Help

    Eclipse gives you a warning about a potential resource leak. You are using a Scanner with System.in, but you don't ever close it. Make it a habit to take good care of your resources. Add a myScanner.close() at the end.

    In your program it won't really matter, because after the System.out.println() the program exits, but for more substantial programs this could be an issue.

    Funny how Eclipse would like to "fix' it. Just ignore the warnings and consider it fixed. Wow.
    IamShahir likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: "myScanner" never close. Need Help

    System.in is a resource (an InputStream), you can read bytes from it; your myScanner object uses that resource; the resource could be an InputStream you have to pay for, per minute. You never release that resource, so it can be a money drain; that's why the compiler warns you: you should close your scanner object when you're done with it.

    kind regards,

    Jos

    edit: I need more espresso coffee, quick; I'm slow again ...
    IamShahir likes this.
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    IamShahir is offline Newbie
    Join Date
    Mar 2016
    Location
    Malaysia
    Posts
    3
    Rep Power
    0

    Default Re: "myScanner" never close. Need Help

    Quote Originally Posted by SurfMan View Post
    Eclipse gives you a warning about a potential resource leak. You are using a Scanner with System.in, but you don't ever close it. Make it a habit to take good care of your resources. Add a myScanner.close() at the end.

    In your program it won't really matter, because after the System.out.println() the program exits, but for more substantial programs this could be an issue.

    Funny how Eclipse would like to "fix' it. Just ignore the warnings and consider it fixed. Wow.
    Can you give me the correct code? I can't run the code too. :(

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: "myScanner" never close. Need Help

    what about
    Java Code:
    myScanner.close()
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: "myScanner" never close. Need Help

    Quote Originally Posted by IamShahir View Post
    Can you give me the correct code? I can't run the code too. :(
    What do you mean?
    That warning will not prevent you running your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: "myScanner" never close. Need Help

    Quote Originally Posted by Tolls View Post
    What do you mean?
    That warning will not prevent you running your code.
    In his first post he said "I can run the code", so I am assuming he changed it without showing us.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: "myScanner" never close. Need Help

    You can always have the compiler ignore the resource and potential resource warnings in Eclipse.

    Go to Windows > Java > Compiler > Errors/Warnings

    You can also turn them off depending on the circumstances.

    @SuppressWarnings("resource")
    Scanner scan = new Scanner(System.in);

    Having told you that, I recommend you leave them alone. They are there to help you.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    IamShahir is offline Newbie
    Join Date
    Mar 2016
    Location
    Malaysia
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  4. Close "One" Window In Java!
    By Alerhau in forum New To Java
    Replies: 1
    Last Post: 06-14-2011, 03:58 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •