I'm suppossed to create a code that user only inputs 1 and 2 OR 99 to exit the program.
if user had put wrong number, then gently ask them again...
However, when i put 1, it keeps displaying "you've selected 1" and never ends.
Also, when i press wrong number such as 44, then it shows message, but the program does not ask me again.
what would be simple way to handle exception?
public static void main(String[] args){
printMenu();
try{
Scanner keyboard = new Scanner(System.in);
int menu = keyboard.nextInt();
while(menu != 99) {
if (menu == 1){
System.out.println("you've selected menu 1\n");
}
else if (menu == 2){
System.out.println("you've selected menu 2\n");
}
else if (menu >=8 || menu <=98 || menu >99){
throw new WrongMenu(menu);
}
}
}
catch(WrongMenu e){
System.out.println(e.getMessage());
}
finally{
System.out.println("all of Exception handling completed.");
System.exit(0);
}
AND the WrongMenu Exception Class
public class WrongMenu extends Exception {
WrongMenu(){
super();
}
WrongMenu(String s){
super(s);
}
WrongMenu(int n){
System.out.println("you've entered : "+ n);
System.out.println("please reenter: ");
}
}