I'm learning to program in Java and I'm working on a pizza-ordering console program.
I've gotten the entire program to work, but I want to fool-proof it. Three places I ask for an int input, but when I type a char or a string, the program crashes.
What I hoped to achieve:
- When the user enters a valid number, the program continues - Done
- When the user enters a char or a string they get an error message and the loop starts over.
Here's an example from my code:
do {
correctInput = true;
System.out.println("Step 1: Look through the menu below and type in the NUMBER of the pizza you want.\n");
pizzaMenu();
System.out.print("\nType in your pizza number here: ");
pizzaNumber = pizza.nextInt();
pizza.nextLine();
switch (pizzaNumber) {
case 1:
pizzaChoice = "Napoli";
pizzaPrice = 50;
break;
case 2:
pizzaChoice = "Hawaii";
pizzaPrice = 50;
break;
case 3:
pizzaChoice = "Quattro Stagioni";
pizzaPrice = 60;
break;
case 4:
pizzaChoice = "Sicillia";
pizzaPrice = 75;
break;
case 5:
pizzaChoice = "Turbo";
pizzaPrice = 60;
break;
case 6:
pizzaChoice = "Jamaica";
pizzaPrice = 75;
break;
case 7:
pizzaChoice = "Romano";
pizzaPrice = 60;
break;
case 8:
pizzaChoice = "Vulcano";
pizzaPrice = 75;
break;
case 9:
pizzaChoice = "Vegetariana";
pizzaPrice = 60;
break;
case 10:
pizzaChoice = "Salame";
pizzaPrice = 60;
break;
default:
System.out.println("You've entered a wrong number. Try again");
correctInput = false;
break;
}
} while (!correctInput);
Could you please help pointing me towards a possible solution?