0

If i loop at option1 = 2 it will keep printing the whole menu. i want it to loop back to the top just once instead of it repeatedly print it. just cannot seem to do this while loop properly. If i loop at option1 = 2 it will keep printing the whole menu. i want it to loop back to the top just once instead of it repeatedly print it. just cannot seem to do this while loop properly.

int option1 = 0;
    
while(option1 != -1){
    mainMenu();        //assuming i have the main menu function
    while(valid2 == false){
        System.out.println("Please Enter a Option: ");
        // Get input from keyboard
        strOption1 = in1.nextLine();
        try{
            option1 = Integer.parseInt(strOption1);
            valid2 = true;
        }
        catch(NumberFormatException e){
        System.out.println("Error, Please Enter Again. ");
}        
}




if (option1 == 1){    
int option2 = 0;

    while (option2 != -1){
        System.out.println("1.Add a new Hawker Food Stall ");
        System.out.println("2.Delete a specific Hawker Food Stall ");
        System.out.println("Please Enter a Option: ");
        Scanner in = new Scanner(System.in);

        while(valid3 == false){
            // Get input from keyboard
            strOption2 = in.nextLine();
            try{
                option2 = Integer.parseInt(strOption2);
                valid3 = true;
            }   
            catch(NumberFormatException e){
            System.out.println("Error, Please Enter Again. ");
            }
        }

        if (option2 == 1){
        System.out.println("E1rror, Please Enter Again. ");
        
        

        }
        else if (option2 == 2){
        System.out.println("E2rror, Please Enter Again. ");
        }
        else if (option2 == 3){
        }
        else {
        System.out.println("Error, Please Enter Again.");
        }
        }

}

else if (option1 == 2){
    System.out.println("This is option 2");
    break;
}
else if (option1 == 3){
    System.out.println("This is option 3");
}
else if (option1 == 4){
    System.out.println("This is option 4");
}
else if (option1 == 5){
    System.out.println("This is option 5");
   }
else if (option1 == 6){
    System.out.println("This is option 6");
}
else if (option1 == 7){
    System.out.println("This is option 7");
}
else if (option1 == 8){
    System.out.println("System Exitting...");
    break;
}
else {
    System.out.println("Error, Please Enter Again.");
}
}

0 Answers0