0
public void runMenu() { int x = 1; Scanner Option = new Scanner(System.in); int Choice = 0; do { try { System.out.println("Choose Option"); System.out.println(""); System.out.println("1: Create Account"); System.out.println("2: Check Account"); System.out.println("3: Take Action"); System.out.println("4: Exit"); System.out.println("Please choose"); Choice = Option.nextInt(); switch (Choice) //used switch statement instead of If else because more effective { case 1: CreateAccount(); break; //breaks iteration case 2: selectAccount(); break; case 3: break; } } catch (Exception e) { System.out.println("Cant do that"); } } while (Choice < 4); System.out.println("Bye"); } 

The exception handling doesnt work e.g. if I enter say outide the range such as 5 it should say something like "Cant do that". Should I enter a if statement defining the condition if so how can I go about doing that? right now it just prints "Bye" if I enter wrong key.

5
  • Input outside of your range doesn't cause an exception. Exceptions are triggered by an input that causes a problem when processed.This might help you tutorialspoint.com/java/java_exceptions.htm Commented Feb 24, 2016 at 0:29
  • There is no part in your code that tells it to throw an exception if the input is an unexpected value. Maybe use the default: case of the switch. (Whether using exceptions here in the first place is correct is debatable) Commented Feb 24, 2016 at 0:31
  • The break applies to the switch; not the loop. Commented Feb 24, 2016 at 0:32
  • What about a incorrect input e.g. if you set an if statement (Choice<4) can it be done then to display user an error and return back to menus? Commented Feb 24, 2016 at 0:33
  • "if I enter say outide the range such as 5 it should say something like "Cant do that"" <-- what exactly makes you think that the current code will do that? .nextInt() will try and grab any valid integer from the input. 5 is valid... Commented Feb 24, 2016 at 0:36

2 Answers 2

1

Your issue is due to the fact that your looping while Choice < 4.

You should change your loop to while(true); and then inside your switch case, create a case 4 which exits the program using System.exit(0)

Then include a default which will inform the user that they input an invalid value

public void runMenu() { int x = 1; Scanner Option = new Scanner (System.in); int Choice = 0; do{ try{ System.out.println("Choose Option"); System.out.println(""); System.out.println("1: Create Account"); System.out.println("2: Check Account"); System.out.println("3: Take Action"); System.out.println("4: Exit"); System.out.println("Please choose"); Choice= Option.nextInt(); switch (Choice) //used switch statement instead of If else because more effective { case 1: CreateAccount(); break; //breaks iteration case 2: selectAccount(); break; case 3: break; case 4: System.out.println("Bye"); System.exit(0); default: System.out.println("Invalid Entry"); break; } } catch (Exception e){ System.out.println("Cant do that"); } } while (true); } 
Sign up to request clarification or add additional context in comments.

Comments

0

Currently you are not throwing any exception.

If you want to throw an exception. Please create default case and throw exception from it

Made default case as below

default: System.out.println("Invalid Entry"); throw new Exception(); 

1 Comment

.nextInt() from Scanner can throw plenty of exceptions. And they are all unchecked to boot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.