6

I'm trying to write a very simple number guessing game (code is below). After 1 round is finished, the user is supposed to be able to decide whether he/she wants to play another round or not. Problem is, the program always skips the last question (never letting the user answer 'y' or otherwise. What am I missing here? Is there something about java.util.Scanner I don't know about?

import java.util.Random;
import java.util.Scanner;

public class GuessNum {

public GuessNum() {         

        int numRandom = 0;    
        int numGuess;    
        int life = 5;    
        String want = "";    
        Random rand = new Random();    
        Scanner scan = new Scanner(System.in);

        do {
            int lifeLeft = 5;
            numRandom = rand.nextInt(9)+1;

            System.out.print("\nGuess the Number [1..10]\n");
            System.out.print("===================\n");
            System.out.print("You have " + lifeLeft + " chances.\n");

            do {
                do {
                    System.out.print("What number do I have in mind: ");
                    numGuess = scan.nextInt();

                    if (numGuess < 1 || numGuess > 10)    
                        System.out.println("Invalid input. Range is 1-10.");    
                } while (numGuess < 1 || numGuess > 10);

                if (numGuess != numRandom && lifeLeft != 0)
                    System.out.println("Wrong! You only have " + --lifeLeft + " chances left.");

            } while (numGuess!=numRandom && lifeLeft > 0);

            if (numGuess == numRandom)
                System.out.println("Correct! -- in " + (life - lifeLeft) + " guess(es).");

            if (lifeLeft == 0) {
                System.out.println("You have no more lives..");
                System.out.println("This is the number: " + numRandom);
            }

            System.out.print("\nEnter 'y' if you want to play again or any other character to exit: ");
                want = scan.nextLine();
        } while (want.equals("y") || want.equals("Y"));
    }

    public static void main(String[] args) {            
        new GuessNum();
    }
}
bow
  • 2,245
  • 4
  • 19
  • 25
  • After a decade of complaints about how difficult it is to simply fetch a bit of input from the console, Sun gave us the `Scanner` class so "Beginning Java" texts could write succinct examples. But `Scanner` is far from foolproof. I usually take the trouble to explicitly read and scan lines via `BufferedReader` etc. – Carl Smotricz Jul 13 '10 at 11:58
  • For more on using `Scanner` and validating input (e.g. integers, vowels-only, etc), see: http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner/ – polygenelubricants Jul 13 '10 at 12:53

2 Answers2

15

Use want = scan.next(); instead of nextLine().

The reason for your problem is that following the preceding nextInt(), you're still on the same line, and nextLine() returns the rest of the current line.

Here's a smallest snippet to reproduce the behavior:

Scanner sc = new Scanner(System.in);
System.out.println("nextInt() = " + sc.nextInt());
System.out.println("nextLine() = " + sc.nextLine());

When you type in, say, 5 and then hit Enter, the output is:

nextInt() = 5
nextLine() = 

That is, nextLine() did not block for your input, because the current line still has an empty string remaining.

For comparison, when you type in, say 5 yeah! and then hit Enter, then the output is:

nextInt() = 5
nextLine() =  yeah!

Note that " yeah!" actually comes from the same line as the 5. This is exactly as specified in the documentation:

String 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.


On half-open ranges

Assuming that the number to guess is between 1 and 10 inclusive, the following code is "wrong":

numRandom = rand.nextInt(9)+1; // this can only be in 1..9 range inclusive!

Here's an excerpt from the documentation of java.util.Random:

int nextInt(int n): Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

That is, like a lot of methods in Java's API, Random.nextInt(int) uses the half-open range, with inclusive lower bound and exclusive upper bound.

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
  • 1
    Ah, thanks (for the random generator correction, too)! I thought nextLine() starts by default at a new line. Should pay more attention to the documentation next time. – bow Jul 13 '10 at 13:15
2

Use scan.next()+ scan.nextLine(); instead eg.

Scanner scan = new Scanner(System.in);

String s = scan.nextLine() +scan.nextLine();

Problem occurs because the last newline character for the last line of input is still queued in the input buffer and the next nextLine() will be reading the remainder of the line (which is empty). So, when you use next it goes to the next token, then you can get the remaining input using nextLine()

J. Pichardo
  • 2,819
  • 18
  • 36