skip to Main Content

I’m trying to make the program return to the menu list if any character other than 1 – 11 is selected when prompted "Please enter your Module Choice" using a do while loop…

Currently even if the user doesn’t select a valid option the program just continues to run

I expect after "Please select a valid module" for it to return to the menu list.

Scanner scanner = new Scanner(System.in);
    
public void moduleSelection() {

    
    System.out.println("1t Algorithms");
    System.out.println("2t Advanced Programming");
    System.out.println("3t Computer Architecture and Operating Systems");
    System.out.println("4t Artificial intelligence and Machine Learning");
    System.out.println("5t Computer and Mobile Networks");
    System.out.println("6t Software Engineering");
    System.out.println("7t Big Data Analyics");
    System.out.println("8t Cyber Security Threats");
    System.out.println("9t Research Methods");
    System.out.println("10t Research Project Proposal");
    System.out.println("11t Individual Research Project");
    

    System.out.println("Please entire your Module choice");
    
    int choice;
    
    choice = scanner.nextInt();
    
    switch (choice)
    {
    case 1: System.out.println("Algorithms");
    break;
    case 2: System.out.println("Advanced Programming");
    break;
    case 3: System.out.println("Computer Architecture and Operating Systems");
    break;
    case 4: System.out.println("Artificial intelligence and Machine Learning");
    break;
    case 5: System.out.println("Computer and Mobile Networks");
    break;
    case 6: System.out.println("Software Engineering");
    break;
    case 7: System.out.println("Big Data Analytics");
    break;
    case 8: System.out.println("Cyber Security Threats");
    break;
    case 9: System.out.println("Research Methods");
    break;
    case 10: System.out.println("Research Project Proposal");
    break;
    case 11: System.out.println("Individual Research Project");
    break;
    default: System.out.println("Please select a valid Module");
    break;
    }
    
}

7

Answers


  1. a. Define a variable to verify user has entered a validNumber.
    b. in do while loop, check if number is valid or not

    E.g:

    do{
    System.out.println("Please entire your Module choice");
    
    int choice;
    
    choice = scanner.nextInt();
    boolean invalidChoice=false;
    switch (choice){
    case 1:
    ...
    break;
    case 2:
    ...
    break;
    
    default: 
    invalidChoice=true;
    print ("Please enter valid choice");
    
    } while(invalidChoice);
    
    Login or Signup to reply.
  2. Based on my assumptions as added as part of comments, better to add help text for the user like ” Please entire your Module choice (1-11) “

    int choice=0;
    boolean isChoiceValid ;
    
    public void moduleSelection() {
    while(!isChoiceValid) {
    System.out.println("1t Algorithms");
    System.out.println("2t Advanced Programming");
    System.out.println("3t Computer Architecture and Operating Systems");
    System.out.println("4t Artificial intelligence and Machine Learning");
    System.out.println("5t Computer and Mobile Networks");
    System.out.println("6t Software Engineering");
    System.out.println("7t Big Data Analyics");
    System.out.println("8t Cyber Security Threats");
    System.out.println("9t Research Methods");
    System.out.println("10t Research Project Proposal");
    System.out.println("11t Individual Research Project");
    System.out.println("Please entire your Module choice (1-11)");
    choice = scanner.nextInt();
    if(choice>=1 && choice<=11)
    {
    isChoiceValid =true;
    }
    }
    
    Login or Signup to reply.
  3. You need to keey switch case in side loop.
    Apart from that you need to provide an option to exit from loop.
    I use exist condition as 99 as input.

         public static void main(String[] args) {
    
    
             boolean doYouWantToExit = false;
             Scanner scanner = new Scanner(System.in);
             do {
                 System.out.println("1t Algorithms");
                 System.out.println("2t Advanced Programming");
                 System.out.println("3t Computer Architecture and Operating Systems");
                 System.out.println("4t Artificial intelligence and Machine Learning");
                 System.out.println("5t Computer and Mobile Networks");
                 System.out.println("6t Software Engineering");
                 System.out.println("7t Big Data Analyics");
                 System.out.println("8t Cyber Security Threats");
                 System.out.println("9t Research Methods");
                 System.out.println("10t Research Project Proposal");
                 System.out.println("11t Individual Research Project");
                 System.out.println("99t Do you want to exist");
                 System.out.println("Please entire your Module choice");
                 int choice;
                 choice = Integer.parseInt(scanner.nextLine());
                 switch (choice)
                 {
                 case 1: System.out.println("Algorithms");
                 break;
                 case 2: System.out.println("Advanced Programming");
                 break;
                 case 3: System.out.println("Computer Architecture and Operating Systems");
                 break;
                 case 4: System.out.println("Artificial intelligence and Machine Learning");
                 break;
                 case 5: System.out.println("Computer and Mobile Networks");
                 break;
                 case 6: System.out.println("Software Engineering");
                 break;
                 case 7: System.out.println("Big Data Analytics");
                 break;
                 case 8: System.out.println("Cyber Security Threats");
                 break;
                 case 9: System.out.println("Research Methods");
                 break;
                 case 10: System.out.println("Research Project Proposal");
                 break;
                 case 11: System.out.println("Individual Research Project");
                 break;
                 case 99: 
                     doYouWantToExit = true;
                 break;
                 default: System.out.println("Please select a valid Module");
                 break;
                 }
    
             }while(!doYouWantToExit); 
         }
    
    Login or Signup to reply.
  4. You can do this by simply adding while or do-while loop

    Updated your method. Check Out:

    private void moduleSelection() {
        int choice = -1;
    
        while (true) { 
            System.out.println("1t Algorithms");
            System.out.println("2t Advanced Programming");
            System.out.println("3t Computer Architecture and Operating Systems");
            System.out.println("4t Artificial intelligence and Machine Learning");
            System.out.println("5t Computer and Mobile Networks");
            System.out.println("6t Software Engineering");
            System.out.println("7t Big Data Analyics");
            System.out.println("8t Cyber Security Threats");
            System.out.println("9t Research Methods");
            System.out.println("10t Research Project Proposal");
            System.out.println("11t Individual Research Project");
            System.out.println("0t Exit");
            System.out.println("Please entire your Module choice");
    
            choice = scanner.nextInt();
            switch (choice) {
            case 0:
                System.exit(1);
                break;
            case 1:
                System.out.println("Algorithms");
                break;
            case 2:
                System.out.println("Advanced Programming");
                break;
            case 3:
                System.out.println("Computer Architecture and Operating Systems");
                break;
            case 4:
                System.out.println("Artificial intelligence and Machine Learning");
                break;
            case 5:
                System.out.println("Computer and Mobile Networks");
                break;
            case 6:
                System.out.println("Software Engineering");
                break;
            case 7:
                System.out.println("Big Data Analytics");
                break;
            case 8:
                System.out.println("Cyber Security Threats");
                break;
            case 9:
                System.out.println("Research Methods");
                break;
            case 10:
                System.out.println("Research Project Proposal");
                break;
            case 11:
                System.out.println("Individual Research Project");
                break;
            default:
                System.out.println("Please select a valid Module");
                break;
            }
        }
    }
    
    Login or Signup to reply.
  5. you may change your code like :

    int choice;
    choice = scanner.nextInt();
    
    if(choice>=1&& choice<=11){
    switch (choice)
    { ...
    
    Login or Signup to reply.
  6. You can use a label:

        loop:
        while (true) {
            System.out.println("Please entire your Module choice");
    
            int choice;
    
            choice = scanner.nextInt();
    
            switch (choice) {
                case 1:
                    System.out.println("Algorithms");
                    break loop;
                case 2:
                    System.out.println("Advanced Programming");
                    break loop;
                case 3:
                    System.out.println("Computer Architecture and Operating Systems");
                    break loop;
                case 4:
                    System.out.println("Artificial intelligence and Machine Learning");
                    break loop;
                case 5:
                    System.out.println("Computer and Mobile Networks");
                    break loop;
                case 6:
                    System.out.println("Software Engineering");
                    break loop;
                case 7:
                    System.out.println("Big Data Analytics");
                    break loop;
                case 8:
                    System.out.println("Cyber Security Threats");
                    break loop;
                case 9:
                    System.out.println("Research Methods");
                    break loop;
                case 10:
                    System.out.println("Research Project Proposal");
                    break loop;
                case 11:
                    System.out.println("Individual Research Project");
                    break loop;
            }
        }
    }
    
    Login or Signup to reply.
  7. public void moduleSelection() {
    
    boolean switch;
    
    do{
    
    switch = false;
    
    System.out.println("1t Algorithms");
    System.out.println("2t Advanced Programming");
    System.out.println("3t Computer Architecture and Operating Systems");
    System.out.println("4t Artificial intelligence and Machine Learning");
    System.out.println("5t Computer and Mobile Networks");
    System.out.println("6t Software Engineering");
    System.out.println("7t Big Data Analyics");
    System.out.println("8t Cyber Security Threats");
    System.out.println("9t Research Methods");
    System.out.println("10t Research Project Proposal");
    System.out.println("11t Individual Research Project");
    
    
    System.out.println("Please entire your Module choice");
    
    int choice;
    
    choice = scanner.nextInt();
    
    switch (choice)
    {
    case 1: System.out.println("Algorithms");
    break;
    case 2: System.out.println("Advanced Programming");
    break;
    case 3: System.out.println("Computer Architecture and Operating Systems");
    break;
    case 4: System.out.println("Artificial intelligence and Machine Learning");
    break;
    case 5: System.out.println("Computer and Mobile Networks");
    break;
    case 6: System.out.println("Software Engineering");
    break;
    case 7: System.out.println("Big Data Analytics");
    break;
    case 8: System.out.println("Cyber Security Threats");
    break;
    case 9: System.out.println("Research Methods");
    break;
    case 10: System.out.println("Research Project Proposal");
    break;
    case 11: System.out.println("Individual Research Project");
    break;
    default: System.out.println("Please select a valid Module");
    switch = true;
    break;
    }
    }while(switch)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search