Results 1 to 11 of 11

Thread: Bug?

  1. #1
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default Bug?

    Hi,

    I've noticed an interesting phenomenon with Java while playing around in Eclipse.

    If I use the Scanner class to get a String inside a do-while loop.. everything works fine. However, if I use the String to get an integer.. the loop executes twice.

    For example..

    do
    {
    System.out.print("Enter a number: ");
    int i = sc.nextInt();
    System.out.print(i);
    }while (askAgain("Another Go?"));

    This code.. executes twice.

    However...

    do
    {
    System.out.print("Enter a number: ");
    String s = sc.nextLine();
    System.out.print(i);
    }while (askAgain("Another Go?"));

    This executes once.

    I've used the same askAgain method in dozens of applications, and I've never had a problem with it.

    private static boolean askAgain(String prompt)
    {
    while (true)
    {
    System.out.print(prompt + " (Y or N)");
    String answer = sc.nextLine();
    if (answer.equalsIgnoreCase("Y"))
    return true;
    else if (answer.equalsIgnoreCase("N"))
    {
    System.out.println("Thanks for playing!");
    return false;
    }
    }
    }


    I'm at a loss here.

    I've got a workaround , but it seems a lot of unnecessary code. Is there something inherently different between the nextInt() and nextLine() methods that cause them to behave differently in a do - while loop?

  2. #2
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I meant.. if i use a SCANNER to get an integer.. not a String. Sorry..

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

    Default

    It's not a bug, it's your lack of understanding how the Scanner methods work: the nextInt() method reads a String representation of an int only and doesn't read a new line character sequence; the nextLine() method does 'eat' the new line sequence. b.t.w. your askAgain( ... ) method you've shown us doesn't even compile.

    kind regards,

    Jos
    Last edited by JosAH; 03-17-2011 at 04:20 PM.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I'll be the first to admit my inexperience.

    My ultimate goal is to bring in an integer to do some math operations. I don't mind the workaround.. I'm just curious as to why the nextInt method didn't work.

    The askAgain method works fine. What error did you get trying to compile?

  5. #5
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I tried it with the next() method, and I fully understand now(kinda) :). Thanks.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by allmusic76@gmail.com View Post
    I tried it with the next() method, and I fully understand now(kinda) :). Thanks.
    Good, in the future don't assume there are bugs in matured (and heavily scrutinized) code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I didn't assume there was a bug, dude.

    If you read the last line of my original post, I explicitly asked what the difference between the methods were.

    No reason to be rude.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by allmusic76@gmail.com View Post
    I didn't assume there was a bug, dude.
    Our bad! You title was obviously referring to insects. :rolleyes:

  9. #9
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    No no.. I admit. The word bug in the title was definitely referring to a defect. However it was promptly followed by a question mark. The post itself offered an additional question relating to the methods.

    However, you were accusing me of an assumption. An assumption is a statement. The title "Bug?" is a question. :p

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I fully understand now(kinda)

    Addressing the "kinda": the path to enlightenment here lies in reading the Scanner API documentation.

    Particularly nextLine() - "Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line."

    vs nextInt() - "Returns true if the next complete token matches the specified pattern. A complete token is prefixed and postfixed by input that matches the delimiter pattern. This method may block while waiting for input. The scanner does not advance past any input."

    ----------------

    Many on forums like this have an adverse reaction to bugs, and they find the nonexistent ones the most annoying. So it's a good word to avoid.

    Actually, there's a serious methodological point behind it. Question anything and everything (especially yourself) before even raising the b-word. As Jos says the library code is just too well scrutinised (and our poor miserable selves too prone to error). Also it's our own code that we can do something about.

  11. #11
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the explanation!

    I actually resolved the kinda.

    When I used the next() method, I understood the newline character explanation. What I didn't understand was why it was going through the while loop twice.

    Then it occurred to me that I had created the Scanner object outside of the do while loop.

    I changed my code by putting the Scanner inside the do-while loop, which allowed me to use the nextInt method and avoid all of the unnecessary casting(or parsing as the case may be).

    In regards to the b word. Fair enough. I'm quite green as you can imagine. I literally bought a Java for Dummies book a week ago with no programming experience.. and I've found myself spending a lot of time reading the official documentation and just playing with methods to understand them.

Posting Permissions

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