CW_2434
CW_2434

Reputation: 156

How to loop the code when user entered wrong input

do {
    id = "";
    quantity = 0;
    System.out.print("Enter id : ");
    id = sc.nextLine();
    for (int i = 0; i < food.length; i++){
        if ((id.toUpperCase()).equals(food[i].getMenuID())) {
            correctInput = true;
            do{
                System.out.print("Enter quantity : ");
                quantity = sc.nextInt();
                if (quantity < 1)
                System.out.println("\nInvalid quantity! Please enter again\n");
        else {
                total = quantity * food[i].getPrice();
                System.out.print(total);
        }
            } while (quantity < 1);
                }
        else if (!((id.toUpperCase()).equals(food[i].getMenuID())))
            System.out.println("\nInvalid ID! Please enter again!\n");
        }
} while (correctInput = false);

I want to loop from "Enter id" if user entered a wrong ID, but I just can't figure out how to do it.

I have tried using do while loop by setting boolean to false when wrong input but it is not working.

How can I achieve that with this code?

I am using JCreator.

Upvotes: 0

Views: 94

Answers (1)

Harris_Smozi
Harris_Smozi

Reputation: 26

make sure your correctInput variable is false before your do while code Also it is correctInput == false and not correctInput = false

Upvotes: 1

Related Questions