# Running total little help

• 02-26-2011, 06:39 PM
silverspoon34
Running total little help
can someone give me a few pointers on adding a running total to this

Code:

```import java.util.Scanner; public class arraytest1 {     public static void main(String[] args)     {     Scanner input = new Scanner (System.in);         int[]number = new int[5];         int count = 0;     int answer = 0;         for(count = 0; count < 5; count++)     {         System.out.print(" Input a number:  ");         number[count] = input.nextInt ();     }         for(count = 0; count <5; count++)     //selecting of array print parameters     {         System.out.println(number[count]);     }         System.out.println("Would you like the list printed in reverse(1=yes 2=no)?  ");         answer = input.nextInt();             if(answer == 1)             for(count = 4; count>= 0; count--)             {                 System.out.println(count + "  " + number[count]);             }         else                 for(count = 0; count <5; count++)                 {                   System.out.println(number[count]);                 }             }         }```
• 02-26-2011, 07:37 PM
gcalvin
What does "running total" mean to you? How will you keep track of it? What is its value at the beginning of your program? How will you update it? When and how will you use it?

-Gary-
• 02-27-2011, 12:20 AM
silverspoon34
The purpose of it is to show the total of all numbers inserted in each array like a calculation with total of all arrats showing after it has dealt with the count
• 02-27-2011, 12:22 AM
gcalvin
Quote:

Originally Posted by silverspoon34
The purpose of it is to show the total of all numbers inserted in each array like a calculation with total of all arrats showing after it has dealt with the count

I only see one array, but in any case, you haven't answered my questions. If you answer them, you'll be well on your way toward writing your program.

-Gary-
• 02-27-2011, 09:55 AM
silverspoon34
right 1 running total to me means a total of the sum of array which has 5 empty slots for user input. When displayed in system.out.prinyln it would look similar to below.

25 25
25 50
40 90

and so on

its value as it is to be a total will be 0 as it is best to have it initialised so i do know i will need a double or int prob called total.

got a rough idea that i need to get it to add each of the pots in the array and printing out on the line is ok as i can just instruct it to print out total after count in the same line , it is getting it it to do the calculation incremental for each line from each pot in the array.
• 02-27-2011, 10:04 AM
gcalvin
OK, that seems like a pretty good understanding. Try to add that into your code now. Declare and initialize the variable, and at least make an attempt to do the addition and the output. Show us what you come up with, and then we can see where (if) you're going wrong.

-Gary-
• 02-27-2011, 02:25 PM
silverspoon34
OK getting somewhere I have created variable and get it to print out that on each line which it does as zero but now for the addition, suspect it may have something to do with the count++ getting it to add as it reads each pot into the total but i now stumped been using a book getting on up with java but not seeing it anyway latest codes with additions

Code:

```import java.util.Scanner; public class arraytest1 {     public static void main(String[] args)     {     Scanner input = new Scanner (System.in);         int[]number = new int[5];         int count = 0;     int answer = 0;     int total = 0;         for(count = 0; count < 5; count++)     {         System.out.print(" Input a number:  ");         number[count] = input.nextInt ();     }         for(count = 0; count <5; count++)     //selecting of array print parameters     {         System.out.println(number[count] + " Total  " + total);     }         System.out.println("Would you like the list printed in reverse(1=yes 2=no)?  ");         answer = input.nextInt();             if(answer == 1)             for(count = 4; count>= 0; count--)             {                 System.out.println(count + "  " + number[count]);             }         else                 for(count = 0; count <5; count++)                 {                   System.out.println(count + "  " + number[count] + " Total  " + total);                 }             }         }```
• 02-27-2011, 04:01 PM
gcalvin
Code:

```001 import java.util.Scanner; 002 003 public class arraytest1 004 { 005    public static void main(String[] args) 006    { 007        Scanner input = new Scanner (System.in); 008    009        int[]number = new int[5]; 010    011        int count = 0; 012        int answer = 0; 013        int total = 0; 014    015        for(count = 0; count < 5; count++) 016        { 017            System.out.print(" Input a number:  "); 018            number[count] = input.nextInt (); 019        } 020    021        for(count = 0; count <5; count++) 022        //selecting of array print parameters 023        { 024            System.out.println(number[count] + " Total  " + total); 025        } 026        System.out.println("Would you like the list printed in reverse(1=yes 2=no)?  "); 027        answer = input.nextInt(); 028    029        if(answer == 1) 030            for(count = 4; count>= 0; count--) 031            { 032                System.out.println(count + "  " + number[count]); 033            } 034        else 035            for(count = 0; count <5; count++) 036            { 037                System.out.println(count + "  " + number[count] + " Total  " + total); 038            } 039    } 040 }```
Line 37 makes sense to me. Line 32 is its counterpart -- don't you want a running total there? Lines 21-25 don't make so much sense. Do you really want that extra loop in addition to the two options below?

Updating your total is simple. I hate to spoon-feed it to you, but I don't know how to give you a hint. It's just this:
Code:

`            total += number[count];`
You should do that just before you print it -- so that would be just before line 37, and (I would think) just before line 32.

Initializing your total variable at line 13 will work OK, but it would make more sense to initialize it around line 28, just before you go into your printing loop. You are not using it before that, and it makes more sense to declare it just before you're going to use it.

Also, I notice you are not using braces ({}) with your if/else starting at line 29. This is legal, because you only have one statement each (the for statement) associated with the if and with the else. But it's dangerous practice. It already had you confused with your indentation, and you might try to add code after the closing brace of your for statement at some point, and be very confused why it breaks things so severely. Better practice, particularly while you're learning, is to always use braces with if/else.

Good luck!

-Gary-
• 02-27-2011, 07:13 PM
silverspoon34
Thanks

indentation is a week point i know about got to get that soughted is a nightmare when things dont compile and you got to go hunting.

regards the {} with if/else i used to use them but found a friend doing coding and was not using them so got into habit of not using them but only recently so will go back to the curly's.

great help though and like how you gone about it next time will right what i do in words hopefully it will click must get back into routine of planning program with visio first.
• 02-27-2011, 10:19 PM
gcalvin
I'm glad you got it working. A few other points, mostly about style:

1) Your code as it is now will print like this:
Code:

```0  25 Total  25 1  15 Total  40 2  3 Total  43 3  38 Total  81 4  14 Total  95```
or
Code:

```4  14 Total  14 3  38 Total  52 2  3 Total  55 1  15 Total  70 0  25 Total  95```
Don't you want:

Code:

```1  25 Total  25 2  15 Total  40 3  3 Total  43 4  38 Total  81 5  14 Total  95```
or
Code:

```5  14 Total  14 4  38 Total  52 3  3 Total  55 2  15 Total  70 1  25 Total  95```
? If you choose to do something about this, do it only in the println() statement -- don't mess with your actual logic.

2) Your count and answer variables don't need to be declared and initialized where they are. answer can be declared right on line 27 where you use it. And count can be declared within each loop. That does mean that it's three different count variables, and not the same count variable throughout, but that's actually better, for subtle reasons I won't try to give here.

3) if and for are not methods, so it's better style to put a space between the keyword and the opening parenthesis.

4) Consider using print() instead of println() on line 26, so that the answer appears on the same line with the question.

5) Your class should be called ArrayTest1 rather than arraytest1. It doesn't matter much at this point, where it's just a holder for your main() method, but it's better to develop good habits early.

-Gary-