user3667054
user3667054

Reputation: 191

Java: Not operator in if statement prints "error" and correct statement?

This is driving me bonkers. This is a very simple program. It prints only "error" when operation is wrong, so that's good. But the problem I have is when the correct answer is fed into the console it prints both the correct statement and error. I don't understand why. I am sure it's very easy but I am not seeing it.

import java.util.Scanner;

public class MeowSample{
    public static void main(String[] args) {

    Scanner okay = new Scanner(System.in);
    System.out.println("Please enter a letter ");
    char letter = okay.next().charAt(0);
    if(letter != 'x' || letter != 'y' || letter != 'z' || letter != 'q'){
        System.out.println("Errorrr");
    }
    if(letter == 'x' || letter == 'y' || letter == 'z'|| letter == 'q'){
        System.out.println("Correct!");
        }
    }
}

Thank you in advance!

Upvotes: 0

Views: 207

Answers (2)

user4668606
user4668606

Reputation:

The problem is this condition:

if(letter != 'x' || letter != 'y' || letter != 'z' || letter != 'q')

If letter is not 'x' or not 'y' will always be true, simply because letter can't be both 'x' and 'y'. A bit of boolean algebra shows that:

l != 'x' || l != 'y' || ...
--> bracket NOT
!(l == 'x') || !(l == 'y') || ...
--> apply rule "!A || !B is equivalent to !(A && B)" (De Morgan 2)
!((l == 'x') && (l == 'y') && ...)

It's obvious that ((l == 'x') && (l == 'y') && ...) will always evaluate to false, unless 'x' == 'y' is given - it isn't. Apply the NOT-operator, and the final result of the complete condition will always be true.

The correct logic operator would be && here:

if(letter != 'x' && letter != 'y' && ...

would only evaluate to true, if letter is neither 'x', nor 'y', nor ... . So far for how to fix the problem itself. But in general an approach using mutual exclusive conditions would be more elegant anyways:

if(letter == 'x' || letter == 'y' || letter == 'z'|| letter == 'q'){
    System.out.println("Correct!");
}else{
    System.out.println("Errorrr");
}

Upvotes: 1

cxw
cxw

Reputation: 17041

Your two if statements are not mutually exclusive. Try:

import java.util.Scanner;

public class MeowSample{
    public static void main(String[] args) {

    Scanner okay = new Scanner(System.in);
    System.out.println("Please enter a letter ");
    char letter = okay.next().charAt(0);
    //if(letter != 'x' || letter != 'y' || letter != 'z' || letter != 'q'){
    //    System.out.println("Errorrr");
    //}
    if(letter == 'x' || letter == 'y' || letter == 'z'|| letter == 'q'){ // this one is OK
        System.out.println("Correct!");
    } else { // <-- add the else
        System.out.println("Errorrr");  // Move the error print down here.
    }
}

Your original code prints "Error" if letter is anything at all - not an x, not a y, not a z, or not a q. Your original code then prints "Correct" if letter is x, y, z, or q.

The revised code first checks for the specific letters you want, then prints error in an else block if letter isn't one you wanted.

Upvotes: 3

Related Questions