dimanche 6 décembre 2015

Java : do While loop, all valid inputs accepted at the same time, only one should be

Scanner console = new Scanner(System.in);
String menuInput;
    //if what user inputs isn't "1", "2" or "Q", an error message is shown
    do {
        menuInput = console.next(); 
        if (!menuInput.equals("1") && !menuInput.equals("2") && !menuInput.equals("Q")) {
            System.out.println("Enter Valid Input");
        } 
    } while (!menuInput.equals("1") && !menuInput.equals("2") && !menuInput.equals("Q"));

I have a menu with 3 options, each option is selected by inputting either "1", "2", or "Q", however when any combination of the 3 valid inputs are entered with a space between them e.g. "1 2" it will accept it as a vaild input. Is there a way to make sure it only accepts one valid input at a time?

Aucun commentaire:

Enregistrer un commentaire