ArrowTron
ArrowTron

Reputation: 1

User Input Menu in Java

How would I make it so that if my user inputs a bad input, the program would tell them to try again and get the input to test if the input it good

My code consists of:

public void displayMenu() {
        Scanner scanner = new Scanner(System.in);
        String choice = "0";
        while (!(choice.equals("4"))) {
            System.out.println("1. Display Audio Files");
            System.out.println("2. Display Compact Discs");
            System.out.println("3. Display Vinyl Records");
            System.out.println("4. Exit");
            if (scanner.hasNextInt()) {
                 choice = scanner.nextLine();
                 System.out.println(choice);
                 if (choice.equals("1")) {
                        //Display the audio Files from musicLibrary library
                        //library.displayChoice("af");
                 }
                 if (choice.equals("2")) {
                 //Display the compact Discs from musicLibrary library
                  //library.displayChoice("cd");
                 }
                 if (choice.equals("3")) {
                        //Display the Vinyl Records from musicLibrary library
                  library.displayChoice("vr");
                 }
            }
            else {
             System.out.println("You gave a bad input! Try again!!!");
                choice = scanner.nextLine();
                    //System.out.println("1. Display Audio Files");
                    //System.out.println("2. Display Compact Discs");
                    //System.out.println("3. Display Vinyl Records");
                    //System.out.println("4. Exit");
                    //choice = scanner.nextLine();
             }
                
            }
        
      
        System.out.println("Bye Bye :]");
        scanner.close();
    }

Right now, it would just repeat the system outputs right under the while loop and I am unsure why. I am printing out the input that the user gives, to help debug, but I still don't understand what is going wrong.

Any ideas on how I approach this?

Upvotes: 0

Views: 509

Answers (1)

Max
Max

Reputation: 353

import java.util.Scanner;

public class Main {

    public static void printOptions() {
        System.out.println("1. Display Audio Files");
        System.out.println("2. Display Compact Discs");
        System.out.println("3. Display Vinyl Records");
        System.out.println("4. Exit");
    }

    public static void close(Scanner scanner) {
        System.out.println("Bye Bye :]");
        scanner.close();
    }

    public static void displayMenu() {
        Scanner scanner = new Scanner(System.in);
        String choice;
        printOptions();
        while (true) {
            if (scanner.hasNextInt()) {
                choice = scanner.nextLine();
                switch (choice) {
                    case "1" -> System.out.println("1");
                    case "2" -> System.out.println("2");
                    case "3" -> System.out.println("3");
                    case "4" -> {
                        close(scanner);
                        return;
                    }
                    default -> {
                        System.out.println("You gave a bad input! Try again!!!");
                    }
                }
            } else {
                close(scanner);
                return;
            }
        }
    }

    public static void main(String[] args) {
        displayMenu();
    }
}

Upvotes: 1

Related Questions