Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanner in Java not working

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();
    }
}
like image 552
bow Avatar asked Jul 13 '10 11:07

bow


People also ask

Why Scanner is not working in Java?

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. That is, nextLine() did not block for your input, because the current line still has an empty string remaining.

How do I run a Scanner in Java?

Create a Scanner Object in Java // read input from the input stream Scanner sc1 = new Scanner(InputStream input); // read input from files Scanner sc2 = new Scanner(File file); // read input from a string Scanner sc3 = new Scanner(String str);

What is nextLine () in Java?

The java. util. Scanner. nextLine() method 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.

Why is nextLine not working?

It's because when you enter a number then press Enter , input. nextInt() consumes only the number, not the "end of line". When input. nextLine() executes, it consumes the "end of line" still in the buffer from the first input.


2 Answers

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

  • Are upper bounds of indexed ranges always assumed to be exclusive?
like image 51
polygenelubricants Avatar answered Sep 22 '22 09:09

polygenelubricants


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()

like image 43
Navdeep Jha Avatar answered Sep 19 '22 09:09

Navdeep Jha