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.");
}
}
if (option==char) {mean?case 3anddefaultcases are essentially the same (ignoring theif (option==char)bit, as I don't understand what you're trying to do there), so you can simply get rid of thecase 3case. And if you want the menu to be shown again, then you should use a loop (e.g.,whileloop). Break out of the loop (either via its condition or via abreakstatement) when appropriate.scanner.nextInt()will throw an exception. You can either handle that exception or you could deal in strings directly.