DJM4
DJM4

Reputation: 65

Why is my Java for/if/else statement outputting duplicates

I am practicing using loops in Java and I have created this if/else loop to continually ask if there is another customer, if the user input is

The problem I am having is after I enter the initial bill amount the code asks if there is another customer like it should, but then prematurely asks for their bill amount and then asks if there is another customer a second time?

My main question is, did I structure this wrong? How am I able to get the loop to not output something twice and/or prematurely?

Scanner input = new Scanner(System.in);
double total;

System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();

for(int c = 0; c > -1; c++) {
    System.out.println("Is there another customer?");
    String customer = input.nextLine();

    if (customer.equalsIgnoreCase("Yes")) {
        System.out.println("Please enter their bill.");
        double bill = input.nextDouble();
    } else {
        System.out.println("Okay, let's total your bill amount.");
    }
}

Upvotes: 3

Views: 412

Answers (4)

Matt Jones
Matt Jones

Reputation: 509

for(int c = 0; c > -1; c++) 

This loop will (basically) run forever as c will always be greater than -1. (Actually this loop will run until overflow occurs because the value of c will be too big to fit in the available storage space allocated for this integer. You can refer here for more information: http://en.wikipedia.org/wiki/Integer_overflow)

A cleaner way to structure this would be to do something like:

String answer = "Yes";
while (answer.equals("Yes"))
{
    System.out.println("Please enter your bill amount.");
    double bill0 = input.nextDouble();

    System.out.println("Is there another customer? (Yes or No)");
    answer = input.nextLine();    
}  

Of course, you need to add error handling if the user enters inputs that you are not expecting. Also, this is more pseudocode than a true implementation.

After this while loop is when you would want to total the amount. Also, in the while loop you might want to have a variable keeping the total amount. Something like:

total += bill0; 

after the line:

double bill0 = input.nextDouble(); 

might do the trick.

Upvotes: 6

hiimjames
hiimjames

Reputation: 518

Scanner input = new Scanner(System.in);
double total;

System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();

System.out.println("Is there another customer?");

while((String customer = input.nextLine()).equalsIgnoreCase("Yes")) {

     System.out.println("Please enter their bill.");
     double bill = input.nextDouble();

}

System.out.println("Okay, let's total your bill amount.");

Upvotes: 0

user3871
user3871

Reputation: 12718

I would use a while loop

    while (scannerInput.equals("yes")) 
        //do your thing
    }

Once the scanner input doesn't equal "yes" anymore, it will exit the while and do something else

for loops are more to iterate through a set of data, rather than a while, which is waiting for state change.

Upvotes: 0

Mshnik
Mshnik

Reputation: 7042

Matt Jones is correct - your loop (basically) runs forever - (overflow means it doesn't truly run forever, but it's close enough).

It seems you're trying to break the loop once the user enters "No". That means you don't know how many iterations you're going to need, so a while loop is more suited to this task than a for loop. So let's use a while loop to do that.

Scanner input = new Scanner(System.in);
double total;

System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();

//loop until the user enters the phrase "No".
while(true) {
    System.out.println("Is there another customer?");
    String customer = input.nextLine();

    if (customer.equalsIgnoreCase("Yes")) {
        System.out.println("Please enter their bill.");
        double bill = input.nextDouble();
    } else if(customer.equalsIgnoreCase("No") {
        System.out.println("Okay, let's total your bill amount.");
        break; //End the loop
    } else{
        System.out.println("Sorry, unrecognized input. Please enter yes or no.");
    }
}
//Do post-loop totaling and stuff.

Upvotes: 0

Related Questions