Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Scanner and scannobject.close()

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Scanner and scannobject.close()

    Ok, I have this question that involves the Scanner class. Basically, when you want to perform garbage collection and there is a scanner object. You type scannerobject.close(); in your final clause.
    My question is...if you make a scanner object in a public class method and then at the end you type object.close(), wHY does this affect the scanner in MAIN???? I haven't tried writing this in a try catch block, but I would think the results would be the same.
    Anyways, I'm running a menu in a repitition statement in main. When the method is called, the method asks for user input, does some calculations and returns back to main, except when it gets to
    Hey welcome back to my menu(menu displays options to perform various tasks)
    type name = objectScanner.nextType(); ERROR
    In this case it's a int and nextInt(). int name = object.nextInt();

    Well, when I take out the scannerObject.close(); from my public class method, it works. There is something I am not understanding, I have searched the forums and checked many sites on google and I just want to know why I can't put the scanner.close() object in a public class method. <---even though the scanner object in main is different than the one in the public class method.

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Driver.main(Driver.java:13)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Scanner and scannobject.close()

    Can you make a small simple program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scanner and scannobject.close()

    I just want to know why I can't put the scanner.close() object in a public class method.
    <---even though the scanner object in main is different than the one in the public class method.
    Show us with your code that the "even though" is true.

    Please post your code correctly.

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Scanner and scannobject.close()

    Quote Originally Posted by GregBrannon View Post
    Show us with your code that the "even though" is true.

    Please post your code correctly.
    LOL???

    CLASS DRIVER
    MAIN
    SCANNER input INITIALIZED
    MENU - ENTER CHOICE
    REPITITIOUS MENU LOOP{ CHOICE != SENTINEL
    OPTION();
    OPTION2();

    choice = input.nextInt(); error occurs
    }

    SCANNER CLOSED
    END MAIN
    END CLASS

    CLASS ALIBRARY
    INSTANCE VARIABLES
    SETGET FUNCTIONS

    PUBLIC VOID TAKESOMEVARIABLESANDCALCULATE{
    SCANNER scans INITIALIZED

    SCANNER.CLOSE(); <--take this out and it works...
    }

    The code doesn't matter, why scanner is giving me an error does.

    --- Update ---

    Use your imagination. Why can't I use the Scanner method Close() in a public libary method? Very simple question, I'm not writing code for it.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Scanner and scannobject.close()

    Can you make real code that will compile, execute and show the problem? Pseudo code can't be used for testing real problems.

    Use your imagination. Why can't I use the Scanner method Close() in a public libary method? Very simple question, I'm not writing code for it.
    Without YOUR code, we can not say what the problem with YOUR code is.
    If you want help, write some code.
    Otherwise, good luck.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Scanner and scannobject.close()

    There is no code to write. It's just a question about scanner method close.

    --- Update ---

    It's one of those things you either know or you don't.
    Like I said, I don't need luck, my programs works but I just want to know why it won't let me use close in a public class method.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Scanner and scannobject.close()

    why it won't let me use close in a public class method.
    Without the code, who can say what the problem is.
    Hundreds of pieces of code use the Scanner class without a problem.
    What is different about your code that causes a problem?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Scanner and scannobject.close()

    Ok ok...give me 5 minutes....

    --- Update ---

    import java.util.Scanner;
    class javaForum{
          static final int SENTINEL = -1;
          public static void main(String[] args){
                 Scanner scans = new Scanner(System.in);
                      Forum f = new Forum();
                      system.out.println("Make a choice\n1. firstChoice\n2. secondChoice\n Enter -1 to quit. 
                      int choice = scans.nextInt();
                      while(choice != SENTINEL){
                              if(choice==1){
                                      f.firstChoice();
                              }else if(choice==2){
                                      f.secondChoice();
                              }
                              system.out.println("Make a choice\n1. firstChoice\n2. secondChoice\n Enter -1 to quit. 
                              choice = scans.nextInt(); //error
                       }
                 scans.close();
          }
    }
     
    class Forum{
          public Forum(){
          }
     
          public void secondChoice(){
                 Scanner input = new Scanner(System.in);
     
                 input.close();
          }
     
          public void firstChoice(){
                 Scanner input = new Scanner(System.in);
     
                 input.close();
          }
    }


    --- Update ---

    My only conclusion is that you shoulnd't do this. LOL

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Scanner and scannobject.close()

    Was there an error message?


    The posted code doesn't compile. It has several errors. The errors need to be fixed so the code will compile and execute for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Scanner and scannobject.close()

    I have a lot of user input, and I want to divide and conquer. I want to seperate input and use void methods to take in user data and use return methods in the class to do some calculations with this data as their will be hundreds of options available to the user.

    --- Update ---

    Error message in first post.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Scanner and scannobject.close()

    I got tired of waiting and fixed the compiler errors. Here is the console for a test run:

    D:\JavaDevelopment\Testing\ForumQuestions10>java ScannerCloseProblem
    Make a choice
    1. firstChoice
    2. secondChoice
    Enter -1 to quit.
    1 2 -1
    Make a choice
    1. firstChoice
    2. secondChoice
    Enter -1 to quit.
    hNI=true, hN=true
    Make a choice
    1. firstChoice
    2. secondChoice
    Enter -1 to quit.
    hNI=true, hN=true
    Out of loop closing scans

    D:\JavaDevelopment\Testing\ForumQuestions10>
    hN and hNI are calls to the hasNext and hasNextInt methods.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Scanner and scannobject.close()

    Yea sorry I just wrote it and the forum doesn't act like a compiler. I can't install anything on the computer I'm using.

  13. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scanner and scannobject.close()

    Without Norm's code, just imagining what you posted as "fixed", does what you posted have anything to do with your original question? Where is the code that resembles this remark?
    My question is...if you make a scanner object in a public class method and then at the end you type object.close(), wHY does this affect the scanner in MAIN???? . . . <---even though the scanner object in main is different than the one in the public class method.

  14. #14
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Scanner and scannobject.close()

    public static Scanner input = new Scanner(System.in);

    then in whatever class you want Driver.input.nextInt(); <--or nextDouble Float Line whatever

    Less code, no confusion, 100% of garbage collected in main with close();

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    Without Norm's code, just imagining what you posted as "fixed", does what you posted have anything to do with your original question? Where is the code that resembles this remark?
    I indicated in the pseudocode and code where the error is/was occurring. //Error
    In the first post I indicated what error was being received by the client. I realize there are a few different ways of accomplishing this now with less code and less confusion.

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scanner and scannobject.close()

    Is your latest post a continuation of the question, or is it the answer you've been looking for?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Scanner and scannobject.close()

    I think the problem is the System.in object's close() method is called. Any code that tries to read from Sytem.in will be effected.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scanner and scannobject.close()

    I indicated in the pseudocode and code where the error is/was occurring.
    I know you did, and I know that you became frustrated, and this thread was one of the more difficult and frustrating I've read in quite some time, but the reason we couldn't be more helpful, is because there wasn't a clear reason why you'd be getting the error you described with the code you posted. We couldn't tell what was happening without seeing real code that demonstrated the problem.

    I'm glad you've arrived at a solution. Be well.

  18. #18
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Scanner and scannobject.close()

    Thanks norm. Lol Greg, have you ever read a word you didn't know the meaning to and it drives you insane until you figure out what it means?
    There were so many newbie questions about scanner it was hard to find what I wanted to know by searching google or the forum.
    For re-usability purposes I would put the scanner class inside the public class instead of just main. I don't know how much memory is being leaked by leaving it like that without closing it or if the close() method run at the end of main - kinda like norm said is garbage collecting all scanner objects because they all run through the I/o - System.in...... anyways, I'll look into this new issue on my own, thanks again. I look forward to more complex discussions soon.

  19. #19
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Scanner and scannobject.close()

    You do not need to call close on a scanner if you scan the command line. The command line does not need to be closed.
    The close method is useful when scanning files or network streams, these need to be closed or otherwise "bad things" might happen. (files not being written to, network partners waiting for input that never comes, etc).
    So unless you use System.setIn(...) and change the default input, you do not need to use the close at all in your code.

    Just wanted to throw that in.

Similar Threads

  1. Am I close to having this right? If statements
    By drew_ch in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 9th, 2014, 12:43 PM
  2. close class
    By nillson in forum Java Theory & Questions
    Replies: 1
    Last Post: September 28th, 2013, 01:47 PM
  3. Why can't I close my JFrame ?
    By bartolio in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 20th, 2013, 11:23 AM
  4. Close jframe
    By takamine in forum AWT / Java Swing
    Replies: 4
    Last Post: February 3rd, 2013, 02:16 PM
  5. Is this correct.Am I close? Help please
    By eagle09 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 26th, 2011, 07:26 AM