user2519738
user2519738

Reputation: 43

Java input keeps being empty?

This is a simple question selection, and then answer program:

import java.util.Scanner;

public class Mains {

    static Scanner console = new Scanner(System.in);
    static Tof tof = new Tof();
    static int Ievel = 0;
    static int Input = 0;
    static boolean GAME = true;
    static boolean AT_START = true;
    static boolean IN_QUESTION = false;

    public static void main (String[] args) {
        while (GAME) {
            String InputS = "";

            if (AT_START) {
                System.out.println("Welcome to the game! Please select a number from 1 to 10.");
                AT_START = false;
            }

            if (!IN_QUESTION)               
                Input = console.nextInt();

            if (Input == -1) {
                GAME = false;
                console.close();
            } else {
                String question = tof.getQuestion(Input);
                String answer = tof.getAnswer(Input);

                System.out.println(question);

                IN_QUESTION = true;

                while (IN_QUESTION) {
                    InputS = console.nextLine();
                    if (InputS != console.nextLine()) {
                        if (InputS.equals(answer)) {
                            System.out.println("Correct!");
                        } else {
                            System.out.println("Incorrect. " + InputS + " " + answer);
                        }
                    }
                }
            }
        }
    }
}

Problem:

When entering the IN_QUESTION loop, and writing a answer, it will always be incorrect. That's because the InputS variable is ALWAYS empty, no matter what, while it has console.nextLine() set on it.

Why is it empty? How do I fix this?

In-case you need the other class Tof: http://pastebin.com/Fn5HEpL2

Upvotes: 2

Views: 124

Answers (4)

Marko Topolnik
Marko Topolnik

Reputation: 200148

You call console.nextLine twice. This means that you read a line that you'll check, and another you won't. This is probably not what you are after. Also note that your initial call of nextInt will not consume the newline you pressed after entering the number. You need a nextLine right after that, but before the main loop.

Some general remarks:

  • uppercase names are only for constants, so your variables should be lowercase;
  • you should really be using local variables instead of static ones. Right now this isn't hurting you, but it soon could.

Upvotes: 0

darijan
darijan

Reputation: 9775

The problem is that the new line character was not read by the nextInt() method so it remain in the scanner buffer and when the next time you called nextLine() that character was printed first.

This is how to fix the issue:

//empty the newline character from the scanner
console.nextLine();
while (IN_QUESTION) {
    InputS= console.nextLine();
    if (InputS.equals(answer)) {
        System.out.println("Correct!");
    } else {
        System.out.println("Incorrect. " + InputS + " " + answer);
    }
}

Upvotes: 0

ctn
ctn

Reputation: 2930

You're reading from the console twice. This should work:

while (IN_QUESTION) {
    InputS = console.nextLine();
    if (InputS.equals(answer)) {
        System.out.println("Correct!");
    } else {
        System.out.println("Incorrect. " + InputS + " " + answer);
    }
}

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55589

nextInt doesn't get the line terminator after the integer and you're reading from the console twice (the second time being in the if-statement).

So if you enter:

123
apple

The following happens:

  • Input gets assigned a value of 123
  • InputS gets assigned an empty string
  • InputS gets compared against apple and it is not equal (from InputS != console.nextLine() - I'm not sure why it's there)

You can fix it by:

  • Putting a console.nextLine(); after console.nextInt();
    OR
    Use Input = Integer.parseInt(console.nextLine()) instead of nextInt

  • Removing this - if (InputS != console.nextLine())

Upvotes: 2

Related Questions