2

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

2
  • What are your console inputs? Also, you should refrain from starting your variable names with an upper case. InputS should be inputS and Input should be input according to code conventions. The static variables in upper case should also follow the correct code conventions. All upper case are usually only for constants. oracle.com/technetwork/java/codeconv-138413.html If you follow code conventions it will be much easier for other java developers to decypher your code. Commented Jun 25, 2013 at 12:43
  • I am putting "true", and "false" and random, ones, all are incorrect. and when I print InputS, the print will be empty. Commented Jun 25, 2013 at 12:45

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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);
    }
}

Comments

0

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);
    }
}

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.