1

I want to make the user enter only "y" "Y" "n" or "N". The resulting code asks the user to enter Y or N when they have entered the correct input, the opposite of what I expected by placing the ! in front of input.equalscaseignore(input).


import java.util.Scanner;


class inputVal {

    public static void main(String[] args) {
        String Input;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter Y or N. ");
        Input = keyboard.nextLine();

        while (!Input.equalsIgnoreCase("Y") || !Input.equalsIgnoreCase("N"))
        {
            System.out.println("Please enter Y or N");
            Input = keyboard.nextLine();

        }


        keyboard.close();
    }

}
1
  • the problem is if user input N then !Input.equalsIgnoreCase("Y") become true ..and if user input Y then !Input.equalsIgnoreCase("N") become true so it always go to inside of the while Commented Oct 17, 2014 at 17:24

5 Answers 5

6

It is your or condition, you must change it to and

while (!Input.equalsIgnoreCase("Y") && !Input.equalsIgnoreCase("N"))

Just reading it out loud would help understand the logic:

WHILE Input doesn't equal (ignore case) "Y" AND Input doesn't equal (ignore case) "N"

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

Comments

2

To elaborate on @gtgaxiola's answer:

!Input.equalsIgnoreCase("Y") || !Input.equalsIgnoreCase("N")

Suppose Input is "Y". The left operand of || is false, since Input equals "Y". But the right operand is true, since Input is not equal to "N". And false || true is true, since the result of || is true whenever either operand is true. In fact, this expression is always going to be true no matter what Input is.

That's why you need to use &&:

!Input.equalsIgnoreCase("Y") && !Input.equalsIgnoreCase("N")

because you want the result to be true only if both equalsIgnoreCase calls return false.

Alternatively, you could say

! (Input.equalsIgnoreCase("Y") || Input.equalsIgnoreCase("N"))

See also this article on DeMorgan's Laws.

(By the way, the convention in Java is to use names starting with lower-case letters for variables, i.e. input.)

Comments

1

Your boolean logic is what's killing you here

while (!Input.equalsIgnoreCase("Y") || !Input.equalsIgnoreCase("N"))

You want to be in the loop while the input is not Y AND not N, therefore you need to change it to

while (!Input.equalsIgnoreCase("Y") && !Input.equalsIgnoreCase("N"))

Comments

0

My code logic is rusty but I believe that the issue is in the || operand.

One of the answers will always be FALSE therefor it won't enter the loop correctly.

Comments

0

Here is great tool to trace your code with: http://cscircles.cemc.uwaterloo.ca/java_visualize/#

Comments

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.