Results 1 to 2 of 2
  1. #1
    smvolt is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Issue with Scanner object

    Hello. I'm having an issue with one aspect of a very simple program here. Here is the program:

    //************************************************** **************************************
    // Gamble.java
    //
    // Simple gambling game.
    //************************************************** **************************************

    import java.util.*;

    public class Gamble
    {
    //--------------------------------------------------------------------------------
    // Controls the game of gambling.
    //--------------------------------------------------------------------------------
    public static void main (String[] args)
    {
    int playersCash=100; // total money the player possesses
    int wager; // stores the amount the player would like to wager
    boolean continuePlaying = true;
    int roll, rollGuess;
    String goOn;

    Random generator = new Random();

    Scanner scan = new Scanner(System.in);
    Scanner check = new Scanner(System.in);

    System.out.println ("Welcome to our Casino.");


    System.out.println ("You have $" + playersCash + ". Let's do some gambling.");


    while (continuePlaying)
    {
    System.out.println ("This is a simple game of dice. If you guess the correct number (1-6), "
    + "you win 5x what you wagered.");
    System.out.println ("What roll would you like to guess?");
    rollGuess = scan.nextInt();
    System.out.println ("How much would you like to wager?");
    wager = scan.nextInt();
    System.out.println ("Good luck sir.");

    roll = generator.nextInt(6) + 1;

    System.out.println ("You rolled a " + roll + ".");
    if (roll==rollGuess)
    {
    System.out.println ("Congratulations! You won $" + wager*5 + ".");
    playersCash += wager*5;
    }
    else
    {
    System.out.println ("Sorry. That does not match your guess of " + rollGuess +".");
    System.out.println ("You lost $" + wager + ".");
    playersCash -= wager;
    }
    System.out.println ("You have $" + playersCash + ". Would you like to continue playing?");
    goOn = check.nextLine(); // here is the source of my confusion

    if ( goOn.equals("y"))
    {

    }
    else
    {
    System.out.println ("Farewell, sir. Please come again.");
    continuePlaying = false;
    }

    }

    }

    }

    This program listed here actually works exactly as I want it to. However, I had to create an additional Scanner object (named check) to get it to work.
    In the line goOn = check.nextLine();, I used to have goOn = scan.nextLine();. This would not work, even though the rest of the program was identical to what you see above. Why was I unable to use the Scanner object I had already created (named scan) in this line? For the record, the program does not prompt the user for input and skips to to the farewell message if I replace check with scan in this line. Thanks in advance for the help.

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

    Default Re: Issue with Scanner object

    You have fallen for the Scanner trap. It has been explained numerous times. Basically it is a feature of the nextInt/nextDouble etc methods which only read the next token (number) and does not consume a carriage return. Solution read and throw away the unwanted EOL.

Similar Threads

  1. Scanner object
    By xxsokol32xx in forum New To Java
    Replies: 5
    Last Post: 05-03-2012, 10:11 AM
  2. How do I reset my Scanner object ?
    By fatabass in forum New To Java
    Replies: 7
    Last Post: 01-13-2012, 09:09 PM
  3. Scanner delimiter issue
    By PrinceSendai in forum New To Java
    Replies: 3
    Last Post: 10-05-2010, 09:41 AM
  4. Replies: 3
    Last Post: 09-24-2008, 11:59 PM
  5. Class Scanner looping issue
    By Stev0 in forum New To Java
    Replies: 1
    Last Post: 05-25-2008, 06:53 PM

Posting Permissions

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