Hi Experts,

I want to ask if there is a way if the user input a character it will handle an exception and go back again to the menu but in my code it wont go back to the menu. Thank you

public class main {
    public static void main(String[] args){

            int choice = 0;
        Scanner input = new Scanner(System.in);
        boolean isValidChoice = false;

        Admin adminObject = new Admin();
        Cashier cashierObject = new Cashier();
        do {
            try {
            System.out.println("1. Admin");
            System.out.println("2. Cashier");
            System.out.println("3. Exit");

            System.out.print("Please Enter Required Option from 1 - 6: ");
            if(input.hasNextInt()){
           choice= input.nextInt(); 
           isValidChoice = true;
       }
            choice = input.nextInt();
            System.out.println("");

            switch (choice) {
                case 1:
                    adminObject.Admin();
                    break;
                case 2:
                    cashierObject.Cashier();
                    break;
            }

        } catch (Exception e){
               System.out.println("Please use valid input."); 
         }
        } while (choice != 0);

    }
}

Maybe the problem is that choice is still zero after a failed nextInt(), thus exiting the loop.
Try setting choice to some other vaue (eg -99) in the catch.

Be a part of the DaniWeb community

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