Search code examples
javamenuswitch-statement

How do I revert back to menu after switch case selection Java


Im creating a management program where I need to display a menu with options for the user to select. When the user select case 1 for example, I want to display some output, then automatically return to the main menu to complete more cases. However, every time I select a case, the output is printed but the menu never gets printed again. The program also never terminates unless I manually do so. I thought maybe there was an infinite loop bug so I put a debugging statement "End of list" under the method that runs when the user select 1. In the output, this line never appears either.

Here is my code:

Trip Management class:

public static void main(String[] args) {
    TripManagement tm = new TripManagement();

    // Load data into containers
    tm.loadEmployees();
    tm.loadTrucks();
    tm.loadTrips();

    Scanner scanner = new Scanner(System.in);
    
    boolean more = true;
    int choice = -1;
    
    while(more) {
        
        try {
            // Display the main menu
            System.out.println("-----------------------------------------");
            System.out.println("1. Display all employees");
            System.out.println("2. Display all trucks");
            System.out.println("3. Display all trips");
            System.out.println("4. Find an employee");
            System.out.println("5. Find a truck");
            System.out.println("6. Find a trip");
            System.out.println("7. Add a new employee");
            System.out.println("8. Add a new truck");
            System.out.println("9. Add a new trip");
            System.out.println("10. Save all data into files");
            System.out.println("0. Exit");
            System.out.println("Enter a choice: ");
            
            // Get user choice
            if (scanner.hasNextInt()) {
                choice = scanner.nextInt();
                scanner.nextLine(); // Consume the newline character
            } else {
                // Clear the input buffer
                scanner.nextLine();
                System.out.println("Wrong input. Please enter a valid number.");
                continue;
            }
     
            // Handle user choice
            switch (choice) {
                case 0: 
                    System.out.println("Bye!");
                    more = false;
                    break;
                case 1:
                    tm.outputEmployees();
                    System.out.println("End of list");
                    break;
                case 2:
                    tm.outputTrucks();
                    break;
                case 3:
                    tm.outputTrips();
                    break;
                case 4:
                    // Implement logic for finding an employee
                    break;
                case 5:
                    // Implement logic for finding a truck
                    break;
                case 6:
                    // Implement logic for finding a trip
                    break;
                case 7:
                    // Implement logic for adding a new employee
                    break;
                case 8:
                    // Implement logic for adding a new truck
                    break;
                case 9:
                    // Implement logic for adding a new trip
                    break;
                case 10:
                    // Implement logic for saving data into files
                    break;
                default:
                    System.out.println("Invalid choice. Please select a valid option.");
                    break;
            }
            
        } catch(InputMismatchException e) {
            System.out.println("Wrong input type." + e);
            
        }
        
    } 
}

public void outputEmployees() {
    try(Formatter formatter = new Formatter(System.out)) {
        for(Employee employee : employees) {
            employee.outputFormatter(formatter);
            formatter.format("%n");
        }
    }
}

Employee class:

public void outputFormatter(Formatter output) {
    output.format("Employee Number: %d%n", getNumber());
    output.format("Name: %s%n", name);
    output.format("Date of Birth: %s%n", dob);
    output.format("Address: %s%n", address); 
}

Here is my output when selecting case 1:

-----------------------------------------
1. Display all employees
2. Display all trucks
3. Display all trips
4. Find an employee
5. Find a truck
6. Find a trip
7. Add a new employee
8. Add a new truck
9. Add a new trip
10. Save all data into files
0. Exit
Enter a choice: 
1
Employee Number 1
Employee Number 2
Employee Number 3
...

Solution

  • Don’t close System.out

    The try-with-resources in outputEmployees closes System.out, at least it probably does if Formatter forwards close() correctly. That's probably why you're not seeing any more output.

    // Closing the `Formatter` also closes `System.out` (a `PrintStream`). 
    try(Formatter formatter = new Formatter(System.out)) {…} 
    

    To quote the Javadoc for Formatter#close:

    Closes this formatter. If the destination implements the Closeable interface, its close method will be invoked.

    One way to fix it would be by using a single Formatter instance, just like you've done with your Scanner.