0
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.

1
  • Is the code the one you tried? If not, can you post the do while solution you wrote with boolean? Commented Dec 26, 2019 at 0:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Or just: while (!correctInput); which helps eliminate mistakes like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.