0

This is extremely simple, but I am new to the Java programming language and some guidance would be helpful. I am making a menu where the options are listed. However, I would love for the user to type a character as an option to get a text like the one here and return the user to the selection again.

I present errors by confusing the case with the default.

import java.util.Scanner;

public class selectMenu {

    public static void main(String[] args) { 
        System.out.println("select your option:");
        System.out.println("1) showing today menu");
        System.out.println("2) showing tomorrow menu");
        
        int op = scanner.nextInt();
        
        switch(op) {
            
            case 1 -> System.out.println("TODAY MENU");
            
            case 2 -> System.out.println("TOMORROW MENU");
            
            case 3 ->
                if (option==char) {
                    System.out.println("This is an invalid option.");
                }
                
            
            default -> System.out.println("Opcion invalida.");
            
        }
}
7
  • Please provide a runnable program which shows what you want to do. Commented Mar 1, 2022 at 4:39
  • @tgdavies Of course, sorry for the inconvenience. That's OK? Commented Mar 1, 2022 at 4:47
  • What does if (option==char) { mean? Commented Mar 1, 2022 at 4:48
  • Note your case 3 and default cases are essentially the same (ignoring the if (option==char) bit, as I don't understand what you're trying to do there), so you can simply get rid of the case 3 case. And if you want the menu to be shown again, then you should use a loop (e.g., while loop). Break out of the loop (either via its condition or via a break statement) when appropriate. Commented Mar 1, 2022 at 4:51
  • 2
    If the user enters a non-integer, then the call to scanner.nextInt() will throw an exception. You can either handle that exception or you could deal in strings directly. Commented Mar 1, 2022 at 4:53

1 Answer 1

1

You could try doing something like this:

import java.util.Scanner;
public class selectMenu
{
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);

        menu: while(true)
        {
            System.out.println("select your option:");
            System.out.println("1) showing today menu");
            System.out.println("2) showing tomorrow menu");
            String op = in.nextLine();

            switch(op)
            {
                case "1":
                        System.out.println("TODAY MENU");
                        break menu;
    
                case "2":
                        System.out.println("TOMORROW MENU");
                        break menu;
    
                default:
                        try
                        {
                            Integer.parseInt(op);
                            System.out.println("You typed an int but it isn't 1 or 2");
                        }
                        catch(NumberFormatException e)
                        {
                            if(op.length() > 1) System.out.println("You typed a String");
                            else System.out.println("You typed a char");
                        }
                        break;
            }
        }
    }
}

It takes the next line rather than nextInt() (which will throw an exception if it's not a number), checks if the answer is "1" or "2", then checks if it's a valid int ("3", "4", etc). If not, it checks the length, and tells you it's a char if the length is 1, otherwise a String.

If you don't care about what the user actually entered (char, String, etc), just that they didn't type an int, you could move the try-catch around the scanner input:

import java.util.Scanner;
public class selectMenu
{
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);

        menu: while(true)
        {
            System.out.println("select your option:");
        System.out.println("1) showing today menu");
        System.out.println("2) showing tomorrow menu");
            try
            {
                int op = in.nextInt();
    
            switch(op)
                {
              case 1:
                        System.out.println("TODAY MENU");
                        break menu;
        
                case 2:
                        System.out.println("TOMORROW MENU");
                        break menu;
        
              default:
                        System.out.println("int input, but not 1 or 2");
            }
            }
            catch(java.util.InputMismatchException e)
            {
                System.out.println("non-int input");
            }
            in.nextLine();
        }
    }
}

Sorry if the indenting is a bit off, I don't know why that was happening.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.