Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By kneitzel
  • 1 Post By JosAH

Thread: Asking user for input twice

  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    34
    Rep Power
    0

    Default Asking user for input twice

    I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.

    //Create Scanner object
    Scanner keys = new Scanner(System.in);

    //Get chips
    System.out.print("How much money would you like to change? ");
    int chips = keys.nextInt();
    System.out.println("You now have $" + chips + " in chips.");

    //What game does the player want to play?
    System.out.print("What game do you want to play? ");
    String typeOfGame = keys.nextLine();

    if (typeOfGame.matches("D|d|Dice|dice")) {
    System.out.println("You picked dice.");
    } else {
    System.out.println("You did not pick dice.");
    }

    *****This is what I get when I run it
    run:
    How much money would you like to change? 50
    You now have $50 in chips.
    What game do you want to play? You did not pick dice.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Asking user for input twice

    You should call next() and not nextLine(). The call to next is blocking till you entered the data.

    Konrad
    csanch11 likes this.

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

    Default Re: Asking user for input twice

    Quote Originally Posted by kneitzel View Post
    You should call next() and not nextLine(). The call to next is blocking till you entered the data.
    That's not it: the nextInt() (and nextDouble()) method only reads a number and no characters following it; that leaves a <newline> character in the input buffer, so a call to nexLine() will read an empty line; the cure is simple though: add a dummy nextLine() call after having read a number.

    kind regards,

    Jos
    csanch11 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    34
    Rep Power
    0

    Default Re: Asking user for input twice

    Thank you so much!!!! so helpful

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Asking user for input twice

    Hi Jos,

    Thank you for the clarification. The documentation was a little misleading because nextLine didn't mention that it might block (and others have this inside their documentation).

    In my tests, calling next worked but the reason is, that it is token based and delimiters are removed. So the newline is no problem.
    So an input of "33 dd" would first give the 33 on nextInt and a next would give "dd" and the nextLine instead of next would give " dd".

    That was quite interesting in my eyes.

    Konrad

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  2. user input on gui
    By JoePenguin in forum New To Java
    Replies: 5
    Last Post: 01-26-2012, 07:27 PM
  3. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  4. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 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
  •