Aaron Minshall
Aaron Minshall

Reputation: 13

How do I go back to my previous menu?

I've looked at everything mentioned about this and if I make a do/while loop it will just repeat the selection. If I make them conditionals instead of a switch it gives me "NoSuchElementException: No line found". Now its also giving me a "NoSuchElementException: No line found" even though I am back to a switch. I just want to know what I'm missing in this code that will let the user back out their first selection (while loop) to make a different one. Here is the code:

public class Zoo {
    static FileRead fr = new FileRead();
    private static final Scanner scnr = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {
        while (true) {
            int userChoice = menu();
            while (userChoice == 1) {
                // Select Animal
                int animal = animalSelect();
                String Name = null;
                    switch (animal) {
                        case 1:
                            Name = "Animal - Lion";
                            break;
                        case 2:
                            Name = "Animal - Tiger";
                            break;
                        case 3:
                            Name = "Animal - Bear";
                            break;
                        case 4:
                            Name = "Animal - Giraffe";
                            break;
                        default:
                            userChoice = menu();
                            break;
                    } FileRead.readAnimal(Name);
            }

            while (userChoice == 2) {
                // Select Habitat
                int animal = habitatSelect();
                String Name = null;
                    switch (animal) {
                        case 1:
                            Name = "Habitat - Penguin";
                            break;
                        case 2:
                            Name = "Habitat - Bird";
                            break;
                        case 3:
                            Name = "Habitat - Aquarium";
                            break;
                        default:
                            userChoice = menu();
                            break;
                        }
                    FileRead.readHabitat(Name);
               }

            // Exit Program
            if (userChoice == 3) {
                System.out.println("Thank you!");
                System.exit(0);
                }

            // Error for invalid option
            else {
                System.out.println("ERROR: Invalid Selection");
                }
            }
        }

    private static int habitatSelect() {
        // Habitat Menu
        System.out.println("Which habitat would you like to monitor?");
        System.out.println("1. Penguin Habitat");
        System.out.println("2. Bird Habitat");
        System.out.println("3. Aquarium");
        System.out.println("4. Exit");
        int userChoice = Integer.parseInt(scnr.nextLine());
        return userChoice;
    }

    private static int animalSelect() {
        // Animal Menu
        System.out.println("Which animal would you like to monitor?");
        System.out.println("1. Lion");
        System.out.println("2. Tiger");
        System.out.println("3. Bear");
        System.out.println("4. Giraffe");
        System.out.println("5. Exit");
        int userChoice = Integer.parseInt(scnr.nextLine());
        return userChoice;
    }

    private static int menu() {
        // Main Menu
        System.out.println("WELCOME! Plese choose from the following");
        System.out.println("1. Monitor Animal");
        System.out.println("2. Monitor Habitat");
        System.out.println("3. Exit");
        int userChoice = Integer.parseInt(scnr.nextLine());
        return userChoice;
    }
}

This all reads a from another file in the package. If that code is needed I will also post it.

Upvotes: 1

Views: 3556

Answers (1)

Nicholas K
Nicholas K

Reputation: 15423

Tweak your main method as below

public static void main(String[] args) throws FileNotFoundException {
  while (true) {
     int userChoice = menu();

     switch (userChoice) {
     case 1: // only for animals
        int animal = animalSelect();
        String Name = null;
        switch (animal) {
        case 1:
           Name = "Animal - Lion";
           break;
        case 2:
           Name = "Animal - Tiger";
           break;
        case 3:
           Name = "Animal - Bear";
           break;
        case 4:
           Name = "Animal - Giraffe";
           break;
        default:
           System.out.println("ERROR: Invalid Selection");
           break;
        } 
        if (Name != null)  // read file only if selection is correct
           FileReader.readAnimal(Name);
        break;

     case 2: // only for habitat
        int habitat = habitatSelect();
        String habitatName = null;
        switch (habitat) {
        case 1:
           habitatName = "Habitat - Penguin";
           break;
        case 2:
           habitatName = "Habitat - Bird";
           break;
        case 3:
           habitatName = "Habitat - Aquarium";
           break;
        default:
           System.out.println("ERROR: Invalid Selection");
           break;
        } 
        if (habitatName != null) // read file only if selection is correct
           FileRead.readHabitat(habitatName);
        break;

     case 3 : // only for exit
        System.out.println("Thank you!");
        System.exit(0);

     default:
        System.out.println("ERROR: Invalid Selection");
     }

  }
}

Thus after each sub-menu, the user is returned to the main menu. As for your exception, for now I have added a null check so that the file is read only if the selection is correct.

Also, note that the above code doesn't contain nested loop which increases the performance and also excludes (the slightly messy) recursive call.

Upvotes: 1

Related Questions