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: ");
	}
}

Recommended Answers

All 5 Replies

while(menu != 99)

Encapsulated your various if statements, so I assume 99 is some sort of exit code. If the user enters 1 as you say, when will this ever become 99? (Never). You need to get more inputs until the user inputs a 99.

With the exception handling sections, you are throwing / catching the exception, and then in finally you are doing:

System.exit(0)

This is going to exit, not ask for another input, hence the behaviour you are observing.

Have a think, if you get stuck again I will check back later.

Basically, all I want to do is...

1. printmenu in a loop while the input is not 99

2. show a message for appropriate menu
3. ask user for input again, when they did it wrong

Just put

int menu = keyboard.nextInt();

in the while loop. It will continue asking for input until menu is equal to 99, or

break;

is called.

Also this is wrong:

WrongMenu(int n){
		System.out.println("you've entered : "+ n);
		System.out.println("please reenter: ");
	}

The Exceptions don't get created to print messages. With the above the message will be printed when you do: throw new WringMenu(1); Exceptions need to be caught and handled. Like this:

catch(WrongMenu e){
      System.out.println(e.getMessage());
}

When you this constructor:

WrongMenu(String s){
		super(s);
	}

You set the message to be 's' and when you call the getMessage() you print that argument.
So try something like this:

WrongMenu(int n){
         super("You've entered : "+ n+". Please reenter.");
}

Basically, all I want to do is...

1. printmenu in a loop while the input is not 99

2. show a message for appropriate menu
3. ask user for input again, when they did it wrong

If that is all you want to do, it seems like creating your own exception is a waste of time. It would be easy to have the

1. printMenu
- get user input -
2. print appropriate message if menus was selected
3. print appropriate message if menus was not selected

in a while loop and have breaks out of the while loop when you don't want the loop to continue looping.

while ( true ){
  printMenu();

   //get input
   
   if( input is correct ){
      //Print Message depending on the input that was received (1, 2, 99)
      //do other things you want
      break; //if you want the looping to stop or quit
   }else{
      // input incorrect, print message and loop again where menu will be re-printed
      printErrorMessage(); //please try again
   }
}

Something like that might be what you are looking for. Unless you are really set on using Exceptions and such, and if so the other posts have given you a good direction to helping you fix the problem. I just wanted to show you there might be an easier way to doing it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.