Java - Asking user if they want to continue (only accepting 'y' or 'n')

Viewed 585

This is my first time posting on here, so if it is formatted poorly I apologize. My application is meant to ask the user to enter one of my roomates name, and the application will output information about that roomate. Then, it should ask the user if it wants to continue. I want it to only be able to accept 'y' or 'n'. When it asks if I want to continue, I enter a letter other than 'y' or 'n' and it correctly asks me to enter an acceptable answer. However, if I DO enter 'y' or 'n' it acts as if I entered an unacceptable answer ONLY ONE TIME, and THEN does what it is supposed to do. Any help is very appreciated.

package roomateapp;

import java.util.Scanner;


public class RoomateApp {


public static void main(String[] args) {



    //Initialize scanner to take in roomate name
    Scanner sc = new Scanner(System.in);
    String cont = "y";
    while (cont.equalsIgnoreCase("y")) {

//CODE HERE BUT NOT SHOWN

        //Ask to continue
        System.out.println("Would you like to continue? (y/n)");
        cont = sc.next();
        System.out.println();

        if (!cont.equalsIgnoreCase("y") || !cont.equalsIgnoreCase("n")) {
            while (!cont.equalsIgnoreCase("y") || !cont.equalsIgnoreCase("n")) {

                System.out.println("Please enter 'y' or 'n'");
                System.out.println("Would you like to continue? (y/n)");
                cont = sc.next();
                if (cont.equalsIgnoreCase("y") || cont.equalsIgnoreCase("n")) {
                    break;
                }
            }
        } else {
        }

        System.out.println();
    }
}
}
2 Answers

As @shmosel mentioned in their comment:

if (!cont.equalsIgnoreCase("y") || !cont.equalsIgnoreCase("n")) {

will always be true - using boolean logic, we can tell it is equivalent to

if (!(cont.equalsIgnoreCase("y") && cont.equalsIgnoreCase("n")))

which may make it clearer that it is always true.

You probably want to check that it is neither "y" nor "n", which you could write as:

if (!(cont.equalsIgnoreCase("y") || cont.equalsIgnoreCase("n")))

Or equivalently:

if (!cont.equalsIgnoreCase("y") && !cont.equalsIgnoreCase("n"))

If you want to learn more about why the above are equivalent, check out the Wikipedia page on De Morgan's Laws (which relate to this particular equivalence) or the page on Logical Equivalence more generally

Why don't you just check that you have "y" or "n", instead of checking what you don't have?.

I would initialize cont to null or some other sentinel value, and then loop while cont remains unchanged. I would only assign a value to cont, once you have validated inside of the loop that the value given by the user is either "y" or "n".

Scanner sc = new Scanner(System.in);

    System.out.println("Would you like to continue? (y/n)");

    String cont = null;
    while(cont == null) {
        String unvalidatedString = sc.next();
        if (unvalidatedString.equalsIgnoreCase("y")) {
            cont = unvalidatedString;
            break;
        }
        if (unvalidatedString.equalsIgnoreCase("n")) {
            cont = unvalidatedString;
            break;
        }
        System.out.println("Please enter 'y' or 'n'");
    }

    // This is just added as a test case for you to verify that it's working. Remove from production code. 
    System.out.printf("The value of cont is: %s\n", cont);
Related