0

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?

1 Answers1

0

You can read a string from the scanner and then manually try to convert it to required number (either Integer or Double):

String str = scan.next();

if (isNumeric(str))
    System.out.println("Numeric value: " + Double.parseDouble(str));
else
    System.out.println("Not a numeric");

And method that checks if given string a numeric or not:

private static boolean isNumeric(String str) {
    try {
        Double.parseDouble(str);
        return true;
    } catch(NumberFormatException e) {
        return false;
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35