0
I have a switch
that validates 5 cases, but already in the first if you do not enter a number that is not among 5 of mine case
, it shows that the option is incorrect and closes.
I wish the program could do that and let me go again to type in the correct option.
I would also like to know how to put only integer number in my input, because if I put letter or number that is not integer, gives direct error.
Code:
Scanner keyboard = new Scanner(System.in);
System.out.print("Select Your Option: ");
int item = keyboard.nextInt();
Here if I don’t put from 1 to 5 he goes to the end:
case 5:
{
System.out.print("Water \n");
System.out.print("Select Quantity: ");
int qtyitem = keyboard.nextInt();
System.out.print("Insert the Money: £ ");
int moneyitem = keyboard.nextInt();
break;
}
default:
System.out.printf("Select Item between 1 - 5: ");
break;
}
And the program closes, not giving me the option to come back again, and anything else I put other than a whole number it comes back with this error:
*********Roehampton Vending Machine*********
SNACKS PRICE QUANTITY
1.Crisps £0.75 10
2.Mars Bar £0.70 10
3.Coca Cola £1.00 10
4.Eugenia £0.50 10
5.Water £0.85 10
*********************************************
Select Your Option: g
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at vendingmachine.VendingMachine.main