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 4 of 4

Thread: Scanner problem

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Scanner problem

    The method below is called within a large loop in another method. The code has been boiled down to easily show the problem I'm having. I found that the method worked fine (prompts user for input, gets input, returns input) until I added a statement at the end of the method that closes the Scanner object. When the kb.close() statement is present, the method works the first time through, but subsequent calls to this method only display the prompt and the Scanner object never waits for the user to input anything thereafter. Can someone explain why this occurs and how to fix it?

    Thanks,
    RonW



    public int scanIt()
    {
    int i;
    Scanner kb = new Scanner(System.out);
    System.out.print("==> "); // prompt for input
    if(kb.hasNextInt())
    {
    i = kb.nextInt();
    System.out.println("User entered " + i);
    }
    kb.close();
    }



  2. #2
    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 problem

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

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

    Default Re: Scanner problem

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

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Scanner problem

    Scanner kb = new Scanner(System.out);
    Should that not be in?

    Two other reasons why input is being skipped is the input object 'kb' is not
    being overwritten by the next input. It is a bug I have seen when using Net beans once before.
    To get around this - rest the input to zero at the end of the input cycle.

    Another piece of advice (which I learnt from this forum and kudos and thumbs up to Greg and Norm for this)
    is that if you are only outputting to the console you do not need to "close" the Scanner after the input. It does
    generate a warning on Eclipse - but it's just a reminder that closing Scanner may cause a resource leak if used
    with files/streams etc. You can safely ignore the error when dealing with simple console applications.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. Scanner problem
    By melki0795 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2013, 05:13 AM
  2. Scanner Problem
    By Syahdeini in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 9th, 2012, 08:37 PM
  3. Problem with Scanner
    By Rafal75 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 7th, 2012, 08:34 PM
  4. Scanner problem - need help
    By destructobob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 23rd, 2011, 02:08 AM
  5. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM

Tags for this Thread