Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add option to exit from switch case in Java

Tags:

java

I am creating a menu based program in java using switch case. here are 4 cases:

  1. add record
  2. delete record
  3. update record
  4. Exit

I added break after each case but, what I want to do is to terminate the program when user enter case no 4 so what to do in this case ?

like image 786
Abhijit Kumbhar Avatar asked Sep 20 '25 13:09

Abhijit Kumbhar


2 Answers

Please don't use System.exit. It's like trying to use a chainsaw to slice a tomato. It's a blunt tool that might be useful for emergency situations, but not for normal situations like you're trying to write.

There are a couple better approaches: (1) If you put your loop in a method, where the method's only purpose is to read the user's input and perform the desired functions, you can return from that method:

private static void mainMenu() {
    while(true) {
        char option = getOptionFromUser();
        switch(option) {
            case '1':
                addRecord();
                break;
            case '2':
                deleteRecord();
                break;
            case '3':
                updateRecord();
                break;
            case '4':
                return;
        }
    }
}

Now, whatever program calls mainMenu() has an opportunity to do some cleanup, print a "goodbye" message, ask the user if they want to back up their files before exiting, etc. You can't do that with System.exit.

Another mechanism, besides return, is to use break to exit the loop. Since break also breaks out of a switch, you'll need a loop label:

private static void mainMenu() {
    menuLoop:
    while(true) {
        char option = getOptionFromUser();
        switch(option) {
            ... as above
            case '4':
                break menuLoop;
        }
    }
    ... will go here when user types '4', you can do other stuff if desired
}

Or (as Riddhesh Sanghvi suggested) you can put a condition in the while loop instead of breaking out of it. His answer used a condition based on the option; another idiom I've used a lot is to set up a boolean for the purpose:

private static void mainMenu() {
    boolean askForAnother = true;
    while(askForAnother) {
        char option = getOptionFromUser();
        switch(option) {
            ... as above
            case '4':
               askForAnother = false;
        }
    }
    ... will go here when user types '4', you can do other stuff if desired
}

Or:

private static void mainMenu() {
    boolean done = false;
    do {
        char option = getOptionFromUser();
        switch(option) {
            ... as above
            case '4':
                done = true;
        }
    } while (!done);
}

So you have a lot of options, all better than System.exit.

like image 170
ajb Avatar answered Sep 23 '25 03:09

ajb


If you do not wish to choose either return or System.exit(ExitCode) then put the termination condition in while loop as shown below.
Why to put while(true) and then put return or System.exit instead exploit the boolean check of the while loop to exit it.

private static void mainMenu() {
    int option=0;//initializing it so that it enters the while loop for the 1st time 
    while(option!=4){
        option = getOptionFromUser();
        switch(option) {
            case 1:
                addRecord();
                break;
            case 2:
                deleteRecord();
                break;
            case 3:
                updateRecord();
                break;
            case 4:
                System.out.print("While Loop Terminated");
                break;
        }
    }
    // when user enters 4,
    //Will execute stuff(here the print statement) of case 4 & then
    //... will come here you can do other stuff if desired
}
like image 31
Riddhesh Sanghvi Avatar answered Sep 23 '25 04:09

Riddhesh Sanghvi