# Cummulative sum help

• 02-27-2014, 03:31 AM
Mthom87
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.

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));   }   } }```
• 02-27-2014, 03:44 AM
Mthom87
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.

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));   }   } }```
• 02-27-2014, 03:56 AM
AlexGraal
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
Code:

`previousTotalPrice = totalPrice;`
Then, the next time you go around, simply calculate your totalPrice again, and then you can print out
Code:

`System.out.println("Combined prices: " + totalPrice + previousTotalPrice)`
But then again, I may be misunderstanding the question here.
• 02-27-2014, 04:09 AM
Mthom87
Re: Cummulative sum from a loop
Would that way store it for more that one loop though?
• 02-27-2014, 03:21 PM
KevinWorkman
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?
• 02-27-2014, 05:35 PM
gimbal2
Re: Cummulative sum help
Quote:

Originally Posted by Mthom87
double burgers;
double fries;

Hmmmm, double burgers and double fries.

Quote:

double payment;
Doh!

(sorry, couldn't resist)
• 02-27-2014, 10:40 PM
Mthom87
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?
• 02-27-2014, 10:53 PM
AlexGraal
Re: Cummulative sum from a loop
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.
• 02-27-2014, 11:24 PM
Mthom87
Re: Cummulative sum help
Thanks, I fixed the post.
• 03-04-2014, 03:58 PM
KevinWorkman
Re: Cummulative sum help
Quote:

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?
• 03-04-2014, 10:16 PM
Mthom87
Re: Cummulative sum help
Quote:

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.
• 03-04-2014, 10:36 PM
typedef
Re: Cummulative sum help
Quote:

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.
In this case the statement you have:
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:
Code:

`System.out.println("The current order costs: " + totalPrice + "\nThe total of both orders costs: " + x);`
• 03-04-2014, 11:00 PM
Mthom87
Re: Cummulative sum help
Quote:

Originally Posted by typedef

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
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?
• 03-04-2014, 11:24 PM
typedef
Re: Cummulative sum help
Quote:

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
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.
• 03-05-2014, 12:17 AM
Mthom87
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.

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));   }   }   } }```
• 03-05-2014, 01:17 AM
typedef
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.
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));         }       }   } }```