Answers for "do while statement to validate string input java"

1

validating string input java

boolean quit = false;
do{ // change your while by this

    // your own stuff here


    // then after all your program stuff
    boolean choiceIsOK = false;
    do{
    String userinput = sc.next();
    char choice = userinput.toLowerCase().charAt(0);
    switch(choice){
    case 'y':
        // case y, do nothing, you could even remove that case.
        choiceIsOK = true;
        break;
    case 'n':
        // case n, do something here
        choiceIsOK = false;
        quit = true;
        break;
    default:
        // error or warning
        System.out.println("Type Y or N to respectively continue or quit");
        break;
    }
    }while(!choiceIsOK);
}while (!quit);
Posted by: Guest on July-29-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language