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

Thread: Problem with Scanner ?

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Location
    Edmonton, Canada
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Problem with Scanner ?

    Hi everybody!

    I am brand new to Java and this is my first post here. This is my code for my attempt at a number guessing program. It runs the way I intended except for the part where it asks the user if they would like to continue.

    I had intended for an answer of 'yes' to make the program play another game and have any other response terminate the program. This worked early on as I was building the program but as soon as I used Scanner for input of the users guess I ran into trouble.

    Now when I run the program it plays the game fine once and asks the continue question but does not wait for a response from the user, and just terminates.

    I am thinking the problem lies with my use of Scanner instance myScanner, Line 43. Can I not use this same instance of myScanner to get input for both the int guess and the continue game String?

    import java.util.Scanner;
     
    public class NumberGuess
    {
    	static Scanner myScanner = new Scanner(System.in);
     
    	public static void main(String args[])
    	{
    		String userPlayAgainQuestion;
    		int userGuess = 0;
     
    		System.out.println("Welcome to Number Guess!\n");
     
    		do
    		{
    			int secretNumber = ((int)(Math.floor(Math.random() * 10))) + 1;	// Generate random number between 1-10
     
    			while((userGuess < 1) || (userGuess > 10))	// Loop until userGuess is valid int
    			{
    				System.out.print("Please enter your guess: ");
    				userGuess = myScanner.nextInt();
     
    				if((userGuess < 1) || (userGuess > 10))	// Check that userGuess is valid int
    				{
    					System.out.println("Guess must be between 1-10. Please try again.");
    					continue;
    				}
     
    				if(userGuess == secretNumber)
    				{
    					System.out.println("You guessed it!! The number was " + userGuess + "!!");
    				}
    				else
    				{
    					System.out.println("Sorry the secret number was " + secretNumber + ".");
    				}
     
    			}
     
     
     
    			System.out.print("Do you want to play again? Type \'yes\' to continue: ");
    			userPlayAgainQuestion = myScanner.nextLine();	// Problem here??????					<- Line 43
     
    		}while(userPlayAgainQuestion.equalsIgnoreCase("yes"));
     
    		System.out.println("\n\nThanks for playing! See you soon.");
    	}
    }


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Problem with Scanner ?

    Scanner#nextInt doesn't consume the newLine, which remains in the scanner's buffer and is consumed when you next call Scanner#nextLine. You need a "dummy" nextLine to clear the buffer.
    userGuess = myScanner.nextInt();
    myScanner.nextLine();
    db

  3. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    derekeverett (March 19th, 2010)

  4. #3
    Junior Member
    Join Date
    Mar 2010
    Location
    Edmonton, Canada
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Scanner ?

    Thank you.. exactly what I needed to know.

  5. #4
    Junior Member
    Join Date
    Mar 2010
    Location
    Edmonton, Canada
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Scanner ?

    ok i have done as you suggested and soaked up the newline after the int is entered. This worked, the program now waits for the user to answer the "play again" question. However, now if the user answers "yes" it repeats the "play again" question over and over until the user enters something else and terminates the program.

    I don't think there is an issue with my looping, is there still something I am not understanding about my Scanner usage?

  6. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Problem with Scanner ?

    Well, considering that you're generating the random number inside the loop, it changes every time the loop repeats. Are you sure that's what you wanted?

    db

  7. #6
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Problem with Scanner ?

    It's because of this

    while((userGuess < 1) || (userGuess > 10))

    Your users guess is always going to be between 1 and 10 on the second loop through since you didn't reset it move the userGuess = 0 to inside the first loop to fix this.

    Also I would change your second if to an else if to speed up the programme by a millisecond as it doesn't need to check it if it is outside the range.

    EDIT:
    Changing it to a do while would probably work aswell.
    Last edited by Faz; March 19th, 2010 at 04:26 AM.

  8. The Following User Says Thank You to Faz For This Useful Post:

    derekeverett (March 19th, 2010)

  9. #7
    Junior Member
    Join Date
    Mar 2010
    Location
    Edmonton, Canada
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Scanner ?

    Thanks everyone. I needed to change the initialization to happen inside the do-while loop and everything is fine now.

    I shall play with your other suggestions as well.

Similar Threads

  1. Scanner vs BufferedReader?
    By Bill_H in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 27th, 2009, 09:44 AM
  2. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM
  3. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM
  4. Homework problem (scanner problems)
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 20th, 2009, 06:10 PM
  5. network scanner
    By vivek494818 in forum Java Networking
    Replies: 0
    Last Post: August 17th, 2009, 11:07 PM