1
    System.out.println("Enter the answers pressing enter after every input : ");
    for(int x = 0; x < 20; x++) {
        do {
            studentAnswers[x] = keyboard.next().toUpperCase();
            System.out.println(studentAnswers[x]);
            if (!"A".equals(studentAnswers[x]) || !"B".equals(studentAnswers[x]) ||
                !"C".equals(studentAnswers[x]) || !"D".equals(studentAnswers[x]))
                System.out.println("You entered an invalid input.");
        } while(!"A".equals(studentAnswers[x]) || !"B".equals(studentAnswers[x]) ||
                !"C".equals(studentAnswers[x]) || !"D".equals(studentAnswers[x]));
    }

Above is my code. For whatever reason, the condition for both the if statement and do-while loop are not recognizing the correct inputs. For example, if I input a, it will print "You entered an invalid input." I even had my teacher look at this. Unfortunately, he could not find an answer either. The array studentAnswers is a String, and I even have the line below the input to ensure that the input is being stored correctly.

I would appreciate any help in solving this problem. Thanks in advance!

1 Answer 1

5

You should replace || with && as you need all the conditions to be true to determine the input is invalid. With || only one condition is needed and it always will be true by your condition statement.

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

7 Comments

Worth to mention that "a".equals("A") is false
It worked! Thank you so much! I'm disappointed I didn't notice that after a good hour of staring at my code. However, there is one thing I'd like to point out. if(!"A".equals(studentAnswers[x]) When I was still in the troubleshooting stage, I changed my if statement to this only. It still wasn't working when I changed it to this alone. Why is that? There shouldn't have been an issue with logical operators.
@RedOne Impossible to say without knowing what the exact data used was. Could have been a mis-type for all we know.
@RedOne Why don't you try reproducing that bug for curiosity's sake?
drive.google.com/file/d/0BzlurahyqQgscjNjdDN2alhFRWM/… Here's a link. This is a version of the code right when I started.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.