• (2 Pages)
• 1
• 2

Reputation:

# Adding a Loop To A Program

Posted 03 March 2010 - 10:46 PM

Hi everybody,

I'm having trouble inserting a loop in this food ordering program to allow the user to keep choosing items until they enter "99" and receive the total cost of their order. Can anyone tell me which loop to use and where I should insert it? Thanks a whole lot!

-Will

The Code:
```import java.util.Scanner;

class Ticket {
public static void main(String args[]) throws Exception {
final double BURGER = 1.99;
final double FRIES = 1.69;
final double COKE = 1.49;

// Display the list of shows and prices to the user
System.out.println("1. Burger  \$1.99");
System.out.println("2. Fries   \$1.69");
System.out.println("3. Coke    \$1.49");
System.out.println("99.(to complete order)");

// Prompt for and read in the number of the show

System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
int orderNumber = input.nextInt();

// Prompt for and read in the number of tickets
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
int orderAmount = input.nextInt();

// Use a switch statement to determine what price to use
double price = 0;

{
switch (orderNumber) {

case 1:
price = BURGER;
break;
case 2:
price = FRIES;
break;
case 3:
price = COKE;    }

}

// Calculate and display the subtotal

double subtotal = price * orderAmount;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

// Calculate and display the total
double total = subtotal + (subtotal * .05);
System.out.printf("Total: \$%.2f\n", total);

}
}

```

This post has been edited by pbl: 04 March 2010 - 09:16 PM
Reason for edit:: Code tags added for a newbie

Is This A Good Question/Topic? 0

## Replies To: Adding a Loop To A Program

### #2 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Adding a Loop To A Program

Posted 03 March 2010 - 10:59 PM

Willbo, on 03 March 2010 - 08:46 PM, said:

Hi everybody,

I'm having trouble inserting a loop in this food ordering program to allow the user to keep choosing items until they enter "99" and receive the total cost of their order. Can anyone tell me which loop to use and where I should insert it? Thanks a whole lot!

-Will

The Code:

import java.util.Scanner;

class Ticket {
public static void main(String args[]) throws Exception {
final double BURGER = 1.99;
final double FRIES = 1.69;
final double COKE = 1.49;

// Display the list of shows and prices to the user
System.out.println("1. Burger \$1.99");
System.out.println("2. Fries \$1.69");
System.out.println("3. Coke \$1.49");
System.out.println("99.(to complete order)");

// Prompt for and read in the number of the show

System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
int orderNumber = input.nextInt();

// Prompt for and read in the number of tickets
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
int orderAmount = input.nextInt();

// Use a switch statement to determine what price to use
double price = 0;

{
switch (orderNumber) {

case 1:
price = BURGER;
break;
case 2:
price = FRIES;
break;
case 3:
price = COKE; }

}

// Calculate and display the subtotal

double subtotal = price * orderAmount;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

// Calculate and display the total
double total = subtotal + (subtotal * .05);
System.out.printf("Total: \$%.2f\n", total);

}
}

Use a do-while loop.
```do
{
System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
int orderNumber = input.nextInt();

// Prompt for and read in the number of tickets
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
int orderAmount = input.nextInt();

// Use a switch statement to determine what price to use
double price = 0;

switch (orderNumber)
{

case 1:
price = BURGER;
break;
case 2:
price = FRIES;
break;
case 3:
price = COKE;
}
}while(orderNumber != 99);

```

I also have some suggestions.
For your switch, might I recommend adding the price if you are going to loop it? Also, you should add a "default" that just breaks out in the case of 99. Oh, and another thing, if you are using an order amount option, then in the switch you should multiply by that variable. It would be like this:
```switch (orderNumber)
{

case 1:
price += BURGER * orderAmount;
break;
case 2:
price += FRIES * orderAmount;
break;
case 3:
price += COKE * orderAmount;
default:
break;
}

```

That should do it, though I haven't run it or anything to be entirely sure. Run and then post back if you have any more issues.

This post has been edited by zim1985: 03 March 2010 - 11:01 PM

Reputation:

## Re: Adding a Loop To A Program

Posted 03 March 2010 - 11:35 PM

First of all thank you for the response, I appreciate it.

Ok so the code below is what I have now. The compiler is telling me that "orderNumber", "orderAmount", and "price" cannot be resolved. Do you know what I need to change. Also, will this code make 99 exit the loop or did I forget to add something? Thanks for being patient, I'm pretty Java illiterate.

The Code:
```import java.util.Scanner;

class Ticket {
public static void main(String args[]) throws Exception {
final double BURGER = 1.99;
final double FRIES = 1.69;
final double COKE = 1.49;

// Display the list of shows and prices to the user
System.out.println("1. Burger  \$1.99");
System.out.println("2. Fries   \$1.69");
System.out.println("3. Coke    \$1.49");
System.out.println("99.(to complete order)");

// Prompt for and read in the number of the show

do
{
System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
int orderNumber = input.nextInt();

// Prompt for and read in the number of the item
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
int orderAmount = input.nextInt();

// Use a switch statement to determine what price to use
double price = 0;

switch (orderNumber)
{

case 1:
price += BURGER * orderAmount;
break;
case 2:
price += FRIES * orderAmount;
break;
case 3:
price += COKE * orderAmount;
default:
break;
}
}   while(orderNumber != 99);

// Calculate and display the subtotal
double subtotal = price * orderAmount;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

// Calculate and display the total
double total = subtotal + (subtotal * .05);
System.out.printf("Total: \$%.2f\n", total);

}

```

This post has been edited by pbl: 04 March 2010 - 09:18 PM
Reason for edit:: Code tags added again. One day you will learn :code:

### #4 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Adding a Loop To A Program

Posted 03 March 2010 - 11:56 PM

Ok, you declared the variables inside the do-while loop, so their scope does not extend outside of it.

Declare them outside the loop and then set their values inside as you have now.

```int orderNumber, orderAmount;
double price = 0;

do
{
System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
orderNumber = input.nextInt();

// Prompt for and read in the number of the item
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
orderAmount = input.nextInt();

// Use a switch statement to determine what price to use

switch (orderNumber)
{

case 1:
price += BURGER * orderAmount;
break;
case 2:
price += FRIES * orderAmount;
break;
case 3:
price += COKE * orderAmount;
default:
break;
}
} while(orderNumber != 99);

```

That's just the loop bit with the variables declared correctly. I ran it also. One thing to watch out for is that when you enter "99" it still runs down through to the "Enter amount" input. This needs to be bypassed.

You also had to initialize the price to 0 outside of the loop or the answer will always be 0 because it is reassigned each time through the loop. (the last time through you break out of the loop).

Reputation:

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 01:20 AM

I see, so can I bypass the 99 problem with an if statement or will that not work inside a loop? do I need to use " default: something"? Im a fresh beginner, I apologize if i seem remedial in this.

### #6 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 02:20 AM

Willbo, on 03 March 2010 - 11:20 PM, said:

I see, so can I bypass the 99 problem with an if statement or will that not work inside a loop? do I need to use " default: something"? Im a fresh beginner, I apologize if i seem remedial in this.

Trying using this as your loop beginning (I only went up to before the switch):
```int orderNumber = 0;
int orderAmount = 0;
double price = 0;

do
{
System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
orderNumber = input.nextInt();

// Prompt for and read in the number of the item
if(orderNumber != 99)
{
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
orderAmount = input.nextInt();
}

```

Notice that I initialized all the variables to 0 outside of the loop. The if statement I added only does the 2nd input if 99 is entered. This should work.

This post has been edited by zim1985: 04 March 2010 - 02:21 AM

Reputation:

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 02:42 AM

Thank you! Final issue, the program is not doing arithmetic at the end to total up the entries, it only shows the cost of the last item i enter. How do I make it so the price includes all entries? Do I just need to change the int price to something other than price * orderAmount?

The Code:

// Calculate and display the subtotal
double subtotal = price * orderAmount;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

Perhaps: // Calculate and display the subtotal
double subtotal = input.nextInt() * orderAmount;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

This should be my last question!

### #8 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 02:49 AM

Guest, on 04 March 2010 - 12:42 AM, said:

Thank you! Final issue, the program is not doing arithmetic at the end to total up the entries, it only shows the cost of the last item i enter. How do I make it so the price includes all entries? Do I just need to change the int price to something other than price * orderAmount?

The Code:

// Calculate and display the subtotal
double subtotal = price * orderAmount;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

Perhaps: // Calculate and display the subtotal
double subtotal = input.nextInt() * orderAmount;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

This should be my last question!

```// Calculate and display the subtotal
double subtotal = price;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

// Calculate and display the total
double total = subtotal + (subtotal * .05);
System.out.printf("Total: \$%.2f\n", total);

```

orderAmount was multiplied in and it wasn't necessary. It does work though. Do the math on the side and you will see that it all adds up.

Reputation:

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 02:56 AM

Hmm, for some reason it is still only showing the price of the last item I entered. Something must be missing from the arithmetic, no?

### #10 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 03:02 AM

This is what I have:
```import java.util.Scanner;

class Ticket {
public static void main(String args[]) throws Exception {
final double BURGER = 1.99;
final double FRIES = 1.69;
final double COKE = 1.49;

// Display the list of shows and prices to the user
System.out.println("1. Burger \$1.99");
System.out.println("2. Fries \$1.69");
System.out.println("3. Coke \$1.49");
System.out.println("99.(to complete order)");

// Prompt for and read in the number of the show

int orderNumber = 0;
int orderAmount = 0;
double price = 0;

do
{
System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
orderNumber = input.nextInt();

// Prompt for and read in the number of the item
if(orderNumber != 99)
{
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
orderAmount = input.nextInt();
}

// Use a switch statement to determine what price to use

switch (orderNumber)
{

case 1:
price += BURGER * orderAmount;
break;
case 2:
price += FRIES * orderAmount;
break;
case 3:
price += COKE * orderAmount;
default:
break;
}
} while(orderNumber != 99);

// Calculate and display the subtotal
double subtotal = price;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

// Calculate and display the total
double total = subtotal + (subtotal * .05);
System.out.printf("Total: \$%.2f\n", total);

}
}

```

It works. I did 1 of each item and 2 of each item and the math was all right. Make sure this is what you have.

Reputation:

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 03:06 AM

Your a lifesaver. Thank you very much for your help. Now I'm going to go through all this a million times until I understand how everything works. Thanks again!

### #12 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 03:07 AM

Guest, on 04 March 2010 - 01:06 AM, said:

Your a lifesaver. Thank you very much for your help. Now I'm going to go through all this a million times until I understand how everything works. Thanks again!

Do you want me to do a quick run through with you? I can go over everything we added and why.

Reputation:

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 07:45 PM

That would be great, also, is there a symbol that represents "integer"? I need to make the program say

if(orderNumber != "integer") {
System.out.println("Invalid ")
}

### #14 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 07:55 PM

Ok, so I'll make this simple. I'll split up the final code and explain the changes:

```import java.util.Scanner;

class Ticket {
public static void main(String args[]) throws Exception {
final double BURGER = 1.99;
final double FRIES = 1.69;
final double COKE = 1.49;

// Display the list of shows and prices to the user
System.out.println("1. Burger \$1.99");
System.out.println("2. Fries \$1.69");
System.out.println("3. Coke \$1.49");
System.out.println("99.(to complete order)");

```

Starting here is where things starting changing.
```// Prompt for and read in the number of the show

int orderNumber = 0;
int orderAmount = 0;
double price = 0;

do
{
System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
orderNumber = input.nextInt();

// Prompt for and read in the number of the item
if(orderNumber != 99)
{
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
orderAmount = input.nextInt();
}

```

We added a do-while loop so you could keep adding more orders to the price. We had to declare the variables and initialize them outside of said loop so that they could be accessed by later parts of the program. Then we set it up so that while the order number isn't 99, it keeps looping (the for statement controls this).

```// Use a switch statement to determine what price to use

switch (orderNumber)
{

case 1:
price += BURGER * orderAmount;
break;
case 2:
price += FRIES * orderAmount;
break;
case 3:
price += COKE * orderAmount;
default:
break;
}
} while(orderNumber != 99);

```

We modified the switch statement so that the price was added to with each loop depending on 1) the order and 2) the amount of said order. We added a "default" case for two reasons: 1) it is a good programming habit, and 2) when "99" is entered, the switch statement will be safely bypassed because there is nothing in the "default" case.

```// Calculate and display the subtotal
double subtotal = price;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

// Calculate and display the total
double total = subtotal + (subtotal * .05);
System.out.printf("Total: \$%.2f\n", total);

}
}

```

Finally we modified the end output by getting rid of the multiplication in the subtotal calculation.

I hope this explanation helps.

Reputation:

## Re: Adding a Loop To A Program

Posted 04 March 2010 - 08:09 PM

zim1985, on 04 March 2010 - 06:55 PM, said:

Ok, so I'll make this simple. I'll split up the final code and explain the changes:

```import java.util.Scanner;

class Ticket {
public static void main(String args[]) throws Exception {
final double BURGER = 1.99;
final double FRIES = 1.69;
final double COKE = 1.49;

// Display the list of shows and prices to the user
System.out.println("1. Burger \$1.99");
System.out.println("2. Fries \$1.69");
System.out.println("3. Coke \$1.49");
System.out.println("99.(to complete order)");

```

Starting here is where things starting changing.
```// Prompt for and read in the number of the show

int orderNumber = 0;
int orderAmount = 0;
double price = 0;

do
{
System.out.print("Enter desired item: ");
Scanner input = new Scanner(System.in);
orderNumber = input.nextInt();

// Prompt for and read in the number of the item
if(orderNumber != 99)
{
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
orderAmount = input.nextInt();
}

```

We added a do-while loop so you could keep adding more orders to the price. We had to declare the variables and initialize them outside of said loop so that they could be accessed by later parts of the program. Then we set it up so that while the order number isn't 99, it keeps looping (the for statement controls this).

```// Use a switch statement to determine what price to use

switch (orderNumber)
{

case 1:
price += BURGER * orderAmount;
break;
case 2:
price += FRIES * orderAmount;
break;
case 3:
price += COKE * orderAmount;
default:
break;
}
} while(orderNumber != 99);

```

We modified the switch statement so that the price was added to with each loop depending on 1) the order and 2) the amount of said order. We added a "default" case for two reasons: 1) it is a good programming habit, and 2) when "99" is entered, the switch statement will be safely bypassed because there is nothing in the "default" case.

```// Calculate and display the subtotal
double subtotal = price;
System.out.printf("Subtotal: \$%.2f\n", subtotal);

// Calculate and display the total
double total = subtotal + (subtotal * .05);
System.out.printf("Total: \$%.2f\n", total);

}
}

```

Finally we modified the end output by getting rid of the multiplication in the subtotal calculation.

I hope this explanation helps.

It helps a lot, thanks much. I have a much better understanding now.