# Thread: Cummulative sum help

1. Member
Join Date
Feb 2014
Posts
8
Rep Power
0

## Cummulative sum help

In the code below, under calculations, I have x=totalPrice+xXxXxX. Obviously the x's were just place holders, but I wanted to know if there was any way to add something there so the program would tally the totalPrice, store it in the x variable outside the loop, so when the loop ended it would display the total price from all the loops added together where I have System.out.println("TOTAL OF ALL ORDERS: \$"+ round.format(x)); at the very end.

Java Code:
```import java.util.Scanner;
import java.text.DecimalFormat;
public class ModifyOrderV2TEST
{
public static void main(String [] args)
{
//Rounding
DecimalFormat round = new DecimalFormat("0.00");

// Variable Declarations
double burgerPrice;
double friePrice;
double sodaPrice;
double totalPrice;
double beforeTax;
double tax;
double change;
double burgers;
double fries;
double sodas;
double payment;
double x;

boolean run = true;
while(run)
{
Scanner input = new Scanner(System.in);

// User Input
System.out.println("Enter number of burgers:");
burgers = input.nextDouble();
System.out.println("Enter number of fries:");
fries = input.nextDouble();
System.out.println("Enter number of sodas:");
sodas = input.nextDouble();

// Calculations
burgerPrice = burgers * 1.69;
friePrice = fries * 1.09;
sodaPrice = sodas * .99;
beforeTax = burgerPrice + friePrice + sodaPrice;
tax = (burgerPrice + friePrice + sodaPrice) * .065;
totalPrice = burgerPrice + friePrice + sodaPrice + tax;
x = totalPrice + xXxXxXxXxXxX;

// System Output
System.out.println("Total before tax: \$" + round.format (beforeTax));
System.out.println("Tax added: \$" + round.format (tax));
System.out.println("Total price: \$" + round.format (totalPrice));

//Prompt for payment
System.out.println("Enter payment");
payment = input.nextDouble();
change = payment - totalPrice;

//Output for change
System.out.println("Change Due: \$" + round.format(change));

//Loop Program
System.out.println("More orders? Key Y or N");
String input2 = input.next();
if(input2.equalsIgnoreCase("y"))
{
run = true;
}
if(input2.equalsIgnoreCase("n"))
{
run = false;
}
System.out.println("TOTAL OF ALL ORDERS: \$"+ round.format(x));
}
}
}```
Last edited by Mthom87; 02-27-2014 at 11:28 PM.

2. Member
Join Date
Feb 2014
Posts
8
Rep Power
0

## Cummulative sum from a loop

In the code below, under calculations, I have x=totalPrice+xXxXxX. Obviously the x's were just place holders, but I wanted to know if there was any way to add something there to the XxXxXx so the program would tally the totalPrice and add it to the previous total price - doing so for a user determined amount of loops when they key "Y" or "N", store that total in the x variable outside the loop, so when the loop ended it would display down where I have it println.

Java Code:
```import java.util.Scanner;
import java.text.DecimalFormat;
public class ModifyOrderV2TEST
{
public static void main(String [] args)
{
//Rounding
DecimalFormat round = new DecimalFormat("0.00");

// Variable Declarations
double burgerPrice;
double friePrice;
double sodaPrice;
double totalPrice;
double beforeTax;
double tax;
double change;
double burgers;
double fries;
double sodas;
double payment;
double x;

boolean run = true;
while(run)
{
Scanner input = new Scanner(System.in);

// User Input
System.out.println("Enter number of burgers:");
burgers = input.nextDouble();
System.out.println("Enter number of fries:");
fries = input.nextDouble();
System.out.println("Enter number of sodas:");
sodas = input.nextDouble();

// Calculations
burgerPrice = burgers * 1.69;
friePrice = fries * 1.09;
sodaPrice = sodas * .99;
beforeTax = burgerPrice + friePrice + sodaPrice;
tax = (burgerPrice + friePrice + sodaPrice) * .065;
totalPrice = burgerPrice + friePrice + sodaPrice + tax;
x = totalPrice + xXxXxXxXxXxX;

// System Output
System.out.println("Total before tax: \$" + round.format (beforeTax));
System.out.println("Tax added: \$" + round.format (tax));
System.out.println("Total price: \$" + round.format (totalPrice));

//Prompt for payment
System.out.println("Enter payment");
payment = input.nextDouble();
change = payment - totalPrice;

//Output for change
System.out.println("Change Due: \$" + round.format(change));

//Loop Program
System.out.println("More orders? Key Y or N");
String input2 = input.next();
if(input2.equalsIgnoreCase("y"))
{
run = true;
}
if(input2.equalsIgnoreCase("n"))
{
run = false;
}
System.out.println("TOTAL OF ALL ORDERS: \$"+ round.format(x));
}
}
}```
Last edited by Mthom87; 02-27-2014 at 11:22 PM.

3. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: Cummulative sum from a loop

I'll preface this by saying I didn't read your code - you need to put [code ] and [ / code ] around your code so it is readable.

But to answer the question that I read, if I understand you correctly.

Simply play around with saving various numbers to various variables. AKA, at the end of your loop, you can have something like
Java Code:
`previousTotalPrice = totalPrice;`
Then, the next time you go around, simply calculate your totalPrice again, and then you can print out
Java Code:
`System.out.println("Combined prices: " + totalPrice + previousTotalPrice)`
But then again, I may be misunderstanding the question here.

4. Member
Join Date
Feb 2014
Posts
8
Rep Power
0

## Re: Cummulative sum from a loop

Would that way store it for more that one loop though?

5. ## Re: Cummulative sum help

When posting code, please use the code tags to preserve formatting.

You asked a question about whether something was possible. The answer to that question is yes. What have you tried?

6. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Cummulative sum help

Originally Posted by Mthom87
double burgers;
double fries;
Hmmmm, double burgers and double fries.

double payment;
Doh!

(sorry, couldn't resist)

7. Member
Join Date
Feb 2014
Posts
8
Rep Power
0

## Re: Cummulative sum help

I tried way too many ways to list them all out, was my question as to what I wanted the variable to do not clear enough? And I'm sorry about the code not being properly formatted, I'm not sure how to post it in a "Java" style way. How would I do that?

8. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: Cummulative sum from a loop

Java Code:
```int previousPrice = 0;

for(int i = 0; i < 57; i++) {
do whatever here;
previousPrice = price;
}```
If the value is declared outside of the loop, then yes, it'll save between runs of the loop.

9. Member
Join Date
Feb 2014
Posts
8
Rep Power
0

## Re: Cummulative sum help

Thanks, I fixed the post.

10. ## Re: Cummulative sum help

Originally Posted by Mthom87
Thanks, I fixed the post.
Does this mean you've solved your problem? If not, what does your updated code do?

11. Member
Join Date
Feb 2014
Posts
8
Rep Power
0

## Re: Cummulative sum help

Originally Posted by KevinWorkman
Does this mean you've solved your problem? If not, what does your updated code do?
Sorry, I should've added quotes, I meant that I fixed the original post to be in JavaScript. I still never figured out how to get it to add up properly.

12. Member
Join Date
Feb 2014
Posts
52
Rep Power
0

## Re: Cummulative sum help

I wanted to know if there was any way to add something there to the XxXxXx so the program would tally the totalPrice and add it to the previous total price - doing so for a user determined amount of loops when they key "Y" or "N", store that total in the x variable outside the loop, so when the loop ended it would display down where I have it println
I might be misunderstanding you but I think what your trying to do is show the price of the current order and show another price showing the price of the sum of all orders placed before.
Your x is your sum of all orders. Your total order variable is your current order.
In this case the statement you have:
Java Code:
```x = totalPrice + xXxXxXxXxXxX;

// should be

x = x + totalPrice;     // or x += totalPrice; which is the same thing```
When you go about displaying our total for the sum of all orders and previous orders all you must do is have a simple print statement:
Java Code:
`System.out.println("The current order costs: " + totalPrice + "\nThe total of both orders costs: " + x);`
If I am misunderstanding your goal please rephrase your question more explicitly.

13. Member
Join Date
Feb 2014
Posts
8
Rep Power
0

## Re: Cummulative sum help

Originally Posted by typedef
If I am misunderstanding your goal please rephrase your question more explicitly.
There are two halves of the program. The first takes your order and prints outs totals. The second half loops the program to run again as many times as the user keys "y" and takes a running tally of the totalPrice variable from all loops put together (the cumulative totalPrice gets stored in the variable x). (At least that's what I want, can't seem to figure that part out lol).

I only want
Java Code:
`System.out.println("TOTAL OF ALL ORDERS: \$"+ round.format(x));`
to display once the user hits "n".

I want the variable x to be a cumulative total of all previous totalPrice, no matter how many times you run the loop.

Clear as mud?
Last edited by Mthom87; 03-04-2014 at 11:04 PM.

14. Member
Join Date
Feb 2014
Posts
52
Rep Power
0

## Re: Cummulative sum help

Originally Posted by Mthom87
There are two halves of the program. The first takes your order and prints outs totals. The second half loops the program to run again as many times as the user keys "y" and takes a running tally of the totalPrice variable from all loops put together (the cumulative totalPrice gets stored in the variable x). (At least that's what I want, can't seem to figure that part out lol).

I only want
Java Code:
`System.out.println("TOTAL OF ALL ORDERS: \$"+ round.format(x));`
to display once the user hits "n".

I want the variable x to be a cumulative total of all previous totalPrice, no matter how many times you run the loop.

Clear as mud?
Interesting saying, never heard it but yes, what you've said is very clear. At the same time it also seems you've answered your own question. Merely place the print state within the no if statement. Make that an statement else if by the way since you don't want to check the if, if the user already said yes.
Also do what I said about assigning x and you should be good to go.
If you have another complication please respond with that problem.

15. Member
Join Date
Feb 2014
Posts
8
Rep Power
0

## Re: Cummulative sum help

Just stuck an x = 0; outside the loop, that fixed it. What a simple solution lol, I've tried a million things, and finally got it to work. Thank you very much! And clear as mud is a military saying btw. Thanks to all of you for you help! I'm adding the final product so if anyone else has a problem with something like this they can learn from my code.

Java Code:
```import java.util.Scanner;
import java.text.DecimalFormat;
public class ModifyOrderV2
{
public static void main(String [] args)
{
//Rounding
DecimalFormat round = new DecimalFormat("0.00");

// Variable Declarations
double burgerPrice;
double friePrice;
double sodaPrice;
double totalPrice;
double beforeTax;
double tax;
double change;
double burgers;
double fries;
double sodas;
double payment;
//Variables for loop
double x;
x = 0;

//Looping by boolean
boolean run = true;
while(run)
{

Scanner input = new Scanner(System.in);

// User Input
System.out.println("Enter number of burgers:");
burgers = input.nextDouble();
System.out.println("Enter number of fries:");
fries = input.nextDouble();
System.out.println("Enter number of sodas:");
sodas = input.nextDouble();

// Calculations
burgerPrice = burgers * 1.69;
friePrice = fries * 1.09;
sodaPrice = sodas * .99;
beforeTax = burgerPrice + friePrice + sodaPrice;
tax = (burgerPrice + friePrice + sodaPrice) * .065;
totalPrice = burgerPrice + friePrice + sodaPrice + tax;
x += totalPrice;

// System Output
System.out.println("Total before tax: \$" + round.format (beforeTax));
System.out.println("Tax added: \$" + round.format (tax));
System.out.println("Total price: \$" + round.format (totalPrice));

//Prompt for payment
System.out.println("Enter payment");
payment = input.nextDouble();
change = payment - totalPrice;

//Output for change
System.out.println("Change Due: \$" + round.format(change));

//Loop Program
System.out.println("More orders? Key Y or N");
String input2 = input.next();
if(input2.equalsIgnoreCase("y"))
{
run = true;
}
if(input2.equalsIgnoreCase("n"))
{
run = false;
System.out.println("TOTAL OF ALL ORDERS: \$"+ round.format(x));
}
}
}
}```
Last edited by Mthom87; 03-05-2014 at 12:31 AM.

16. Member
Join Date
Feb 2014
Posts
52
Rep Power
0

## Re: Cummulative sum help

Cool man. I thought I'd add some little formatting stuff to make your text interface look a little sleeker. Also you can make that declaration and assignment just an initialization to conserve some space.
Java Code:
```import java.util.Scanner;
import java.text.DecimalFormat;
public class ModifyOrderV2
{
public static void main(String [] args)
{
//Rounding
DecimalFormat round = new DecimalFormat("0.00");

// Variable Declarations
double burgerPrice;
double friePrice;
double sodaPrice;
double totalPrice;
double beforeTax;
double tax;
double change;
double burgers;
double fries;
double sodas;
double payment;
// Initialization to avoid: "error: variable x might not have been initialized"
double x = 0;

//Looping by boolean
boolean run = true;
while(run)
{
Scanner input = new Scanner(System.in);

// User Input
System.out.print("Enter number of burgers: ");
burgers = input.nextDouble();
System.out.print("Enter number of fries: ");
fries = input.nextDouble();
System.out.print("Enter number of sodas: ");
sodas = input.nextDouble();
System.out.println();

// Calculations
burgerPrice = burgers * 1.69;
friePrice = fries * 1.09;
sodaPrice = sodas * .99;
beforeTax = burgerPrice + friePrice + sodaPrice;
tax = (burgerPrice + friePrice + sodaPrice) * .065;
totalPrice = burgerPrice + friePrice + sodaPrice + tax;
x += totalPrice;

// System Output
System.out.println("Total before tax: \$" + round.format (beforeTax));
System.out.println("Tax added: \$" + round.format (tax));
System.out.println("Total price: \$" + round.format (totalPrice));
System.out.println();

//Prompt for payment
System.out.print("Enter payment: ");
payment = input.nextDouble();
change = payment - totalPrice;

//Output for change
System.out.println("Change Due: \$" + round.format(change));
System.out.println();

//Loop Program
System.out.print("More orders? Key Y or N:    ");
String input2 = input.next();
System.out.println();
if (input2.equalsIgnoreCase("y"))
{
run = true;
}
else if (input2.equalsIgnoreCase("n"))
{
run = false;
System.out.println("TOTAL OF ALL ORDERS: \$"+ round.format(x));
}
}
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•