0

I'm having a terribly tough time with this simple code. My while conditions are always ignored and the print statement is executed. Please help.

package Checkpoints;
import java.util.Scanner;


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

        Scanner keyboard = new Scanner(System.in);

        /**
         * Write an input validation that asks the user to enter 'Y', 'y', 'N', or 'n'.
         */


        String input, Y = null, N = null;

        System.out.println("Please enter the letter 'Y' or 'N'.");
        input = keyboard.nextLine();


        while (!input.equalsIgnoreCase(Y) || !(input.equals(N)))
                //|| input !=y || input !=N ||input !=n)

            {
            System.out.println("This isn't a valid entry. Please enter the letters Y or N" );
            input = keyboard.nextLine();
            }

    }

}
3
  • 1
    You are never assigning values to Y or N, and then using them in a compare. Commented Jul 27, 2017 at 15:43
  • 1
    Your Y and N are null objects. Nothing is equal to a null object. Commented Jul 27, 2017 at 15:43
  • Try stepping through the code with a debugger. Commented Jul 27, 2017 at 15:44

3 Answers 3

1

Change this;

String input, Y = null, N = null;

to this;

String input, Y = "Y", N = "N";

So that you can compare the user input string with "Y" and "N" strings.

And this;

while (!input.equalsIgnoreCase(Y) || !(input.equals(N)))

to this;

while (!(input.equalsIgnoreCase(Y) || input.equalsIgnoreCase(N)))

because your design of condition is misaimed, as @talex warned.

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

4 Comments

Also !input.equalsIgnoreCase(Y) || !(input.equals(N)) is wrong. It should be !(input.equalsIgnoreCase(Y) || input.equalsIgnoreCase(N)).
@talex Correct. Thank for the heads up pal.
If you add it to your question it become complete, but useless because question must be deleted as off-topic anyway,
Added the new condition.
0

You are comparing the input to null since you forgot to define the values of the string Y and N.

You can define the answer values in constants like so:

public static final String YES = "y";
public static final String NO  = "n";

public static void main (String[] args) {
    Scanner keyboard;
    String  input;

    keyboard = new Scanner(System.in);

    System.out.println("Please enter the letter 'Y' or 'N'.");
    input = keyboard.nextLine();

    while (!(input.equalsIgnoreCase(YES) || input.equalsIgnoreCase(NO))) {
        System.out.println("This isn't a valid entry. Please enter the letters Y or N" );
        input = keyboard.nextLine();
    }
}

EDIT: corrected the while condition as suggested by talex

Comments

0

Add this extra conditions before "while" loop to avoid this

    if(Y!= null && !Y.isEmpty()) 
    if(N!= null && !N.isEmpty())

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.