validating string input java

Solutions on MaxInterview for validating string input java by the best coders in the world

showing results for - "validating string input java"
Denzel
22 Aug 2019
1boolean quit = false;
2do{ // change your while by this
3
4    // your own stuff here
5
6
7    // then after all your program stuff
8    boolean choiceIsOK = false;
9    do{
10    String userinput = sc.next();
11    char choice = userinput.toLowerCase().charAt(0);
12    switch(choice){
13    case 'y':
14        // case y, do nothing, you could even remove that case.
15        choiceIsOK = true;
16        break;
17    case 'n':
18        // case n, do something here
19        choiceIsOK = false;
20        quit = true;
21        break;
22    default:
23        // error or warning
24        System.out.println("Type Y or N to respectively continue or quit");
25        break;
26    }
27    }while(!choiceIsOK);
28}while (!quit);
29