0

Hi guys so my program doesn't really catch any errors i.e. when I input a letter instead of a valid number it does catch the error but it doesn't return back to the menu , it just displays the statement. And when I use a number outside of the switch statement i.e. 5 it just loops back to the menu without displaying error. My code is below:

public void runMenu() {
    Scanner Option = new Scanner (System.in);
    int x = 1;
    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:
         Menu();
         int choice = UserInput();
         performAction(choice);
        break;
    case 4:
        System.out.println("Thanks for using the application"); 
        System.exit(0);
    default:
        throw new Exception();
    // x=2; //if code doesn't run successfully then x !=2 leading to exception
} 
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
John
  • 117
  • 1
  • 2
  • 13
  • 1
    Why it should return to the menu in case any exception is thrown ? You have written a `return` statement in catch block – Rahman Mar 19 '16 at 16:42
  • John - Note that your last edit to the question has made the code non-compilable ... and once you fix the compilation errors, the behavior will be different. If you want people to help you, don't do that kind of thing. – Stephen C Mar 20 '16 at 03:09

3 Answers3

2

The case 4 is not closed with a break therefore you never instantiate your exception !

You should have this at the end of your switch :

default:
    throw new Exception();
    break;

Also, you need to remove the return from catch section.

    catch (Exception e){
        System.err.println("Enter Correct Input");
        return ;
    }
The Roy
  • 2,178
  • 1
  • 17
  • 33
Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31
  • Further you should remove the return – The Roy Mar 19 '16 at 17:02
  • I removed the return from the catch section an it just goes on a crazy one ., it doesnt stop looping :s – John Mar 19 '16 at 18:09
  • that is only when I input letter it works fine when its a incorrect number – John Mar 19 '16 at 18:10
  • Partly right. However, `case 4` doesn't need a `break` because `System.exit` never returns. Also, the break for the final switch arm does not need a `break` either. Whether you include redundant `break` statements is a stylistic issue ... only. – Stephen C Mar 20 '16 at 03:01
0

This really isn't a situation where an exception is normally thrown. If you want the program to just loop back to the menu if the user enters an invalid option, you should only have to add a default case to your while loop. You don't even need your x integer for that. You can try throwing a new Exception in the default case if you want.

    import javax.swing.*;
    import java.util.Arrays;
    import java.util.Scanner;

    public class runMenu {
        public void runMenu() {


            Scanner Option = new Scanner (System.in);

            int Choice = 0;

                    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:
                            Menu();
                            int choice = UserInput();
                            performAction(choice);

                            break;
                        case 4:
                            System.out.println("Thanks for using the application");
                            System.exit(0);

                            // x=2; //if code doesn't run successfully then x !=2 leading to exception

                            throw new Exception();

                            break;
                        default:
                            System.out.println("Invalid option. Please try              

                            again.");

                             throw new Exception();

                            runMenu();

                    }
                }

        }

    }
Patrick S.
  • 275
  • 1
  • 5
  • 19
0

for when you've entered a number other than 1-4 you should have a default option, to re-run the questions after you've displayed an error message call runMenu()

for example,

 case 4:
        System.out.println("Thanks for using the application"); 
        System.exit(0);
 default:
        println "Choose 1-4";
        runMenu()

Hope this helps.