# Cumulative sum of entries

• November 1st, 2011, 02:55 PM
CSUTD
Cumulative sum of entries
I need a little help.

Not sure where to go from here.

Code :

```public static void main(String[] args) { Scanner keyboard = new Scanner (System.in);   int value; int total;   do { System.out.print("Enter some numbers: "); value = keyboard.nextInt();         System.out.println("\nYou entered numbers " + value + ".\n");     } while ( value != 0 ); }```

After each entry, the program reports the cumulative sum of the entries to date. The program terminates when the user enters a zero. If your program received as data, the values 2, 7, 3 and 0, your program should display 2, 9 and 12. Be sure to label your input and your output in an appropriate manner.
• November 1st, 2011, 04:47 PM
KevinWorkman
Re: Cumulative sum of entries
How would you do this by hand, without a computer? If somebody tells you a list of numbers, how would you keep adding them up? Write out instructions in English, that you could give to your dumbest friend, and you'll have an algorithm that should be pretty easy to translate to code.
• November 1st, 2011, 05:51 PM
CSUTD
Re: Cumulative sum of entries
Is there a simpler way to print everything out on two lines?

Example:
You entered numbers 2,7, 3, and 0.
Your totals are 2, 9, and 12.

Code :

```{ Scanner keyboard = new Scanner (System.in);   int value = 0; int total = 0;   do { System.out.print("Enter some numbers: "); value = keyboard.nextInt();   total = total + value;   System.out.println("\nYou entered numbers " + value + "."); System.out.println("Your total are " + total );   } while ( value != 0 ); }```
• November 1st, 2011, 05:55 PM
KevinWorkman
Re: Cumulative sum of entries
I'm not sure what you mean by simpler, and I'm not convinced that code does what your example says it does.
• November 1st, 2011, 05:59 PM
CSUTD
Re: Cumulative sum of entries
This is my output:
Code :

```Enter some numbers: 2   You entered numbers 2. Your total are 2 Enter some numbers: 7   You entered numbers 7. Your total are 9 Enter some numbers: 3   You entered numbers 3. Your total are 12 Enter some numbers: 0   You entered numbers 0. Your total are 12```
• November 1st, 2011, 06:04 PM
KevinWorkman
Re: Cumulative sum of entries
That's what I would expect your output to be. If you want to print them out on the same line, you're going to have to save them to an array or a String and wait until after the loop to print them.
• November 1st, 2011, 06:06 PM
Junky
Re: Cumulative sum of entries
Quote:

Originally Posted by CSUTD
This is my output:

Good for you. What do you want us to do about it?
• November 1st, 2011, 06:09 PM
Tjstretch
Re: Cumulative sum of entries
You may want to have a look in ArrayList, which can be found here:

ArrayList (Java Platform SE 6)
• November 1st, 2011, 06:19 PM
CSUTD
Re: Cumulative sum of entries
Quote:

Originally Posted by KevinWorkman
That's what I would expect your output to be. If you want to print them out on the same line, you're going to have to save them to an array or a String and wait until after the loop to print them.

Can't use arrays. Even if we were I wouldn't know how. I am in an intro class. I only know to a certain point.

Can I ask why you didn't think my code produced what it should.

Quote:

Originally Posted by Junky
Good for you. What do you want us to do about it?

um, if you would have read the thread you would have known the question. You don't have to answer it or provide help but don't post useless stuff on a thread.

Quote:

Originally Posted by Tjstretch
You may want to have a look in ArrayList, which can be found here:

ArrayList (Java Platform SE 6)

How can I be spoon feeding? It is my thread...I am just asking for help, no one has to give me any advice or help at all.

I don't want it done for me. Just need help sometimes.
• November 1st, 2011, 06:32 PM
Tjstretch
Re: Cumulative sum of entries
Yeah I thought you were spoon feeding because you posted a useless amount of code, I edited my post when I looked at the thread owner.

As a side rant, I really hate it when people say "I can't use that.. I'm just a beginner" because... well that seems counter-intuitive. Is your teacher actually going to give you points off or do you think that there must be some simpler way to do this? If you really can't do arrays, than you can try to append a String like Kevin already said.

To append Strings you can use += (Compiler will automatically convert it to a StringBuilder, most likely)
EG:
Code Java:

```String result = ""; int sum = 3; result += sum+" "; sum += 2; result += sum + " ";```
etc.