Results 1 to 3 of 3
  1. #1
    hhh80p is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Problem with while loop and the scanner method

    hi guys:
    I'm having trouble with this while loop. The thing is I want this loop ask me for a name and then ask for 3 cards, but I want this process 3 time. When first the while starts, there is not problem, it ask me for the name of the player and then ask for 3 cards. But in the second and third time, it show me the system.out.print message, but it bypass the input.Scanner method (meaning it doesn't let me put the name of the player) and go to the second while loop, which is asking me for 3 cards. this is my code. I hope somebody can help me. thanks

    public void letsPlayPoker()
    {
    // Create Scanner to obtain input from command window
    Scanner input = new Scanner(System.in);
    String pokerName;
    int numberCards = 1;
    int counter = 0;
    int cardNumber;


    while (counter <= 2) // how many poker player do we have
    {
    System.out.print("Please enter the name of the poker Player: ");
    pokerName = input.nextLine();
    System.out.printf("Welcome:\n%s\n", pokerName);
    counter = counter + 1;

    while (numberCards <= 2)
    {
    System.out.print("\nEnter card number\n");
    cardNumber = input.nextInt();
    System.out.printf("name", cardNumber);
    numberCards = numberCards + 1;
    }
    if (numberCards == 3)
    numberCards = 1;
    }
    System.out.print("\nGood luck gentlemen!!!!\n");
    }
    }

    thanks

  2. #2
    hhh80p is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    For all the beginner like me:
    Found it!!!!!

    ok, so, if you decide to get a scanner in a while loop, at the end of the loop, you have to put and if statement that will check it the scanner has something, because if it has something, the next time the loop pass for the scanner code, it won't read it since it already has something in there. here is the solution:

    at the end of the loop put

    if (input.hasNextLine()) // name that I select to create a new scanner object
    pokerName = input.nextLine(); // pokerName is the name of my string, and
    // here I created an reset the input scanner.

    cheers!

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

    Default

    In general this type of error occurs when there is still an <enter> character in the input buffer and you want to read an entire line. e.g. if you want to read in int and you type '1', '2', '3', <enter>, the scanner will read the first three characters but leaves the <enter> character in the input buffer because it isn't part of an integer value. If you want to read a String afterwards and you want it by reading an entire line, the method will hit the <enter> character that is still in the input buffer; the readLine method will return immediately after having read an empty line and you programs leaves you with a puzzled look on your face.

    The remedy is simple though: read a dummy empty line first and only then read your String values.

    kind regards,

    Jos

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 07:13 PM
  2. while loop bypasses scanner input on 2nd pass
    By xf021209 in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 09:10 AM
  3. Problem with scanner in loop
    By grifan526 in forum New To Java
    Replies: 2
    Last Post: 07-21-2009, 01:06 AM
  4. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 03:15 AM
  5. Using the scanner method
    By silvia in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:50 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
  •