Results 1 to 10 of 10
  1. #1
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Question Running total little help

    can someone give me a few pointers on adding a running total to this

    Java 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]);
                    }
                }
            }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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-

  3. #3
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    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

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by silverspoon34 View Post
    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-

  5. #5
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    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.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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-

  7. #7
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Red face

    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

    Java 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);
                    }
                }
            }
    Thanks in advance

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    I've added line numbers to your code for reference, and cleaned up your indentation:
    Java 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:
    Java 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-

  9. #9
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Thumbs up

    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.

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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:
    Java Code:
    0  25 Total  25
    1  15 Total  40
    2  3 Total  43
    3  38 Total  81
    4  14 Total  95
    or
    Java 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:

    Java Code:
    1  25 Total  25
    2  15 Total  40
    3  3 Total  43
    4  38 Total  81
    5  14 Total  95
    or
    Java 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-

Similar Threads

  1. Calculating Total?
    By ethemartian in forum New To Java
    Replies: 6
    Last Post: 02-21-2011, 05:09 AM
  2. Running Total by Key Group in HashMap
    By KevMull in forum New To Java
    Replies: 3
    Last Post: 11-16-2009, 04:44 PM
  3. Replies: 0
    Last Post: 08-26-2009, 02:03 PM
  4. Total noob
    By J_Walker in forum New To Java
    Replies: 9
    Last Post: 04-24-2009, 04:10 AM
  5. Printing total out
    By denisdoherty in forum New To Java
    Replies: 1
    Last Post: 04-25-2008, 07:40 AM

Posting Permissions

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