Search code examples
javamenuinvalidation

How do I invalidate a char input in Java?


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.");
            
        }
}

Solution

  • 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.