hi guys a beginner here. can i get some help ? how do i prevent error for eg. i only one Y/y or N/n to be written and when other characters or integers are written, it will show an error message. the default doest seem to be working

switch(Confirm) 
    {

          case 'n':
          case 'N': 
                   EnterTheUsage();break;
          case 'y':
          case 'Y':      
                                       DisplayTheUsage();break;
                     default: System.out.println("Pls enter only Y or N.");
          }

Edited 3 Years Ago by Nick Evan: Fixed formatting

In what way it "doest seem to be working"? That switch looks just fine. Maybe include a break in the default case, though, just for completeness.

sorry if my question wasnt clear. what i meant to say was besides N/n Y/y, if i type in any random thing such as sadfsdsa, the program will still run but it will just repeat prompting for the 2nd method call which is N, as i put the 2nd method call as after the default such as default : S.O.P("whatever"); and then method();.instead of repeating the whole method, any way to make such that it displays an error message and prompts N / Y again instead of straight away making it repeat prompting for the method call?

Edited 6 Years Ago by yasin.mr: n/a

Okay? And what is actually happening? I know what you want, but you aren't telling what you actually have, and so I can't really help you get there from here, since I don't know where here is.

I can say, however, that if you expect the switch to loop, no. You need to use a while loop for that. (Note that this switch can be a part of that while loop, though).

Okay? And what is actually happening? I know what you want, but you aren't telling what you actually have, and so I can't really help you get there from here, since I don't know where here is.

I can say, however, that if you expect the switch to loop, no. You need to use a while loop for that. (Note that this switch can be a part of that while loop, though).

ahhh ok thank you my friend

This article has been dead for over six months. Start a new discussion instead.