john wilson
john wilson

Reputation: 31

How do I stop a do...while loop from being infinite

I have the following code which is resulting in an infinite loop:

System.out.println("Adjust Invoices");
System.out.println("Would like to Pay an invoice or Add an invoice to your account?");
System.out.println("Press '1' to Pay and '2' to Add");

int invoice = choice.nextInt();
do
{
    if (invoice == 1)
    {
        System.out.println("one");
    }
    if (invoice == 2)
    {
        System.out.println("two");
    }
    else
    {
        System.out.println("Press '1' to Pay and '2' to Add");
    }
} while (invoice >= 3 || invoice <= 0);

How can I stop this being an infinite loop when I enter something other than '1' or '2'?

Upvotes: 1

Views: 2778

Answers (3)

Benjamin Basmaci
Benjamin Basmaci

Reputation: 2557

Well, I guess first you will have to put the

int invoice = choice.nextInt();

inside your loop to avoid that. Otherwise there is no sense in using the loop. You want to loop if you put a wrong input, right? Well, that only makes sense if you allow the user to correct their input.

Then, I would just braek as soon as a valid input arises and put the hint print on the end without the "else". Also, if you break at those points, you can remove your condition. It will be redundant. Your Hint is also redundant, so just put in right before the input. So, what you end up with is:

System.out.println("Adjust Invoices");
System.out.println("Would like to Pay an invoice or Add an invoice to your account?");

int invoice;
do
{
    System.out.println("Press '1' to Pay and '2' to Add");
    invoice = choice.nextInt();
    if (invoice == 1)
    {
        System.out.println("one");
        break;
    }
    if (invoice == 2)
    {
        System.out.println("two");
        break;
    }
} while (true);

Upvotes: 1

forpas
forpas

Reputation: 164069

Are you sure that you want to stop the loop when you enter something other than '1' or '2'?
The usual practice is to stop this kind of loop when the user enters a valid selection, in your case 1 or 2 and give another option like 0 to exit.
So as long as the user does not enter 1 or 2 the loop must not finish:

Scanner choice = new Scanner(System.in);
System.out.println("Adjust Invoices");
System.out.println("Would like to Pay an invoice or Add an invoice to your account?");

int invoice;

do {
    System.out.println("Press '1' to Pay or '2' to Add, '0' to Exit");
    invoice = choice.nextInt();
    if (invoice == 0) {
        System.out.println("Exiting...");
        break;
    } else if(invoice == 1) {
        System.out.println("one");
        break;
    } else if (invoice == 2) {
        System.out.println("two");
        break;
    } 
} while(invoice < 0 || invoice > 2);

Upvotes: 0

Gratien Asimbahwe
Gratien Asimbahwe

Reputation: 1614

To stop a loop from being infinite, you must avoid that the condition remains true otherwise it won't stop. If you want to stop the loop after having responded according to the condition, break it.

With your case you can proceed as follows (remember, you must pick another value if the first was not verified, that's why invoice=choice.nextInt() must be included at the top of the loop):

do
     {
          invoice = choice.nextInt();
          if(invoice == 1)
          {
                 System.out.println("one");
                 break;
                 //quit the loop after the operation
          }
          else if (invoice == 2)
          {//we add else if because invoice might be either 1 or 2 or another value and not two values at same time.
                System.out.println("two");
                //quit the loop after the operation
          }
         else
         {
                   System.out.println("Press '1' to Pay and '2' to Add");
                   //don't break here because you are just leaving a message to let the user know invoice must be either one or two and if not display the message
          }
    }while(invoice >= 3 || invoice <=0);

Upvotes: 0

Related Questions