When a validation is negative I would like the program to return, so I can enter the data again

Asked

Viewed 56 times

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

1 answer

0

If you want to validate whether a number has been entered, I suggest reading it all as String, try to convert to int and display a message if you cannot. To do this, I suggest creating an auxiliary method:

public int lerInteiro(Scanner scanner, String mensagem) {
    while (true) { // enquanto não digitar número, continua repetindo
        try {
            System.out.println(mensagem);
            return Integer.parseInt(scanner.nextLine()); // converte para inteiro
        } catch (NumberFormatException e) { // se não digitou número, mostra mensagem e pede que digite novamente
            System.out.println("Type a number");
        }
    }
}

That is, until an integer number is entered, the while continues running, asking you to type again. When a valid integer number is entered, it is returned.

I chose to do so because, although nextInt be very convenient in many cases, he also hides some traps, especially if invalid data are entered.

Then just modify the program to use this method. And if you want something to repeat, use a loop. In the example below, I used while:

Scanner keyboard = new Scanner(System.in);

while (true) { // repete várias vezes
    int item = lerInteiro(keyboard, "Select Your Option (0 to exit): ");
    if (item == 0)
        break; // se digitar zero, sai do while(true)
    switch (item) {
        case 1:
            // faz algo
            break;
        case 2:
            // faz algo
            break;
        case 3:
            // faz algo
            break;
        case 4:
            // faz algo
            break;
        case 5:
            System.out.print("Water \n");
            int qtyitem = lerInteiro(keyboard, "Select Quantity: ");
            int moneyitem = lerInteiro(keyboard, "Insert the Money:  £  ");
            break;
        default:
            System.out.println("Select Item between 1 - 5.");
    }
}

Included an option to interrupt the loop (if you type zero), but then you can adapt as your need.

Thus, he keeps asking to type the option several times. If you enter a number outside the range, he shows the message and asks you to type again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.