Search code examples
javareturnprogram-entry-pointexit

Is it possible to exit from a main method without terminating the program?


I know about the return statement and have tried it. System.exit(0) also does the same. But using it here terminates the program. Is there any way i can use so that if the user types other input except 1-7 , the program doesn't terminate , so that i don't have to recompile and rerun the program ? Or is it not possible in Java ?

import java.util.Scanner;
public class NewShoppingCart{
public static void main(String args[]) {

    boolean flag = true;
    long code;
    String choice;
    NewShop aShop = new NewShop();
    Scanner sc = new Scanner(System.in);
    Integer parse = 0;

    System.out.println("-----ITEM------");
    do {
        System.out.println("1. Display all items");
        System.out.println("2. Search items");
        System.out.println("3. Add items to list");
        System.out.println("4. Add items to cart");
        System.out.println("5. Display cart");
        System.out.println("6. Issue item");
        System.out.println("7. Exit");
        System.out.println("Choice:");
        choice = sc.nextLine();

        try{
         parse = Integer.parseInt(choice);
          }
        catch(Exception e){
            System.out.println("Please enter a valid integer"); 
            return;  
        }
        if (parse >=1 && parse <= 7 )
         {

        switch (parse) {

        case 1:
            aShop.display();
            break;

        case 2:
            aShop.searchItem();
            break;

        case 3:
            aShop.addItem();
            break;

        case 4:
            aShop.addItemtoCart();
            break;


        case 5:
            aShop.displayCart();
            break;

        case 6:
            aShop.issueItem();
            break;

        case 7:
            System.out.println("Thank you!\n");
            flag = false;
            break;

        default :
             System.out.println("Please enter choice relevant to context");
          }
        }
     else   return;
       }

     while (flag != false);
    sc.close();

 }
}

Solution

  • You can never go out of main with just one thread. This is likely an XY problem. What you really want is to go back to the start of the loop if the user inputs something invalid.

    The continue keyword will stop executing the current iteration of the enclosing loop and start a new iteration immediately. This is what you should use in place of return.

        try{
         parse = Integer.parseInt(choice);
          }
        catch(Exception e){
            System.out.println("Please enter a valid integer"); 
            return;  // <--- change this to "continue;"
        }
    

    Also, this:

    if (parse >=1 && parse <= 7 )
        {
    
            switch (parse) {
    
                case 1:
                    aShop.display();
                    break;
    
                case 2:
                    aShop.searchItem();
                    break;
    
                case 3:
                    aShop.addItem();
                    break;
    
                case 4:
                    aShop.addItemtoCart();
                    break;
    
    
                case 5:
                    aShop.displayCart();
                    break;
    
                case 6:
                    aShop.issueItem();
                    break;
    
                case 7:
                    System.out.println("Thank you!\n");
                    flag = false;
                    break;
    
                default :
                    System.out.println("Please enter choice relevant to context");
            }
        }
        else return;
    

    should really be:

        if (parse >=1 && parse <= 7 )
        {
    
            switch (parse) {
    
                case 1:
                    aShop.display();
                    break;
    
                case 2:
                    aShop.searchItem();
                    break;
    
                case 3:
                    aShop.addItem();
                    break;
    
                case 4:
                    aShop.addItemtoCart();
                    break;
    
    
                case 5:
                    aShop.displayCart();
                    break;
    
                case 6:
                    aShop.issueItem();
                    break;
    
                case 7:
                    System.out.println("Thank you!\n");
                    flag = false;
                    break;
            }
        }
        else {
            System.out.println("Please enter choice relevant to context");
            continue;
        }
    

    The "Please enter choice relevant to context" message should really be printed in the else statement. Because in your if, you already checked whether parse is between 1 and 7, so in the switch, parse can't be anything else so the default branch is never reached. After you print the message, you continue; in order to go back to the start of the loop.