Results 1 to 10 of 10
Thread: Running total little help
- 02-26-2011, 05:39 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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]); } } }
- 02-26-2011, 06:37 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-26-2011, 11:20 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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-26-2011, 11:22 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 02-27-2011, 08:55 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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, 09:04 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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, 01:25 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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
Thanks in advanceJava 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, 03:01 PM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I've added line numbers to your code for reference, and cleaned up your indentation:
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?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 }
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:
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.Java Code:total += number[count];
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, 06:13 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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, 09:19 PM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
orJava Code:0 25 Total 25 1 15 Total 40 2 3 Total 43 3 38 Total 81 4 14 Total 95
Don't you want:Java Code:4 14 Total 14 3 38 Total 52 2 3 Total 55 1 15 Total 70 0 25 Total 95
orJava Code:1 25 Total 25 2 15 Total 40 3 3 Total 43 4 38 Total 81 5 14 Total 95
? If you choose to do something about this, do it only in the println() statement -- don't mess with your actual logic.Java Code:5 14 Total 14 4 38 Total 52 3 3 Total 55 2 15 Total 70 1 25 Total 95
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
-
Calculating Total?
By ethemartian in forum New To JavaReplies: 6Last Post: 02-21-2011, 04:09 AM -
Running Total by Key Group in HashMap
By KevMull in forum New To JavaReplies: 3Last Post: 11-16-2009, 03:44 PM -
Problem in running Java swing wizard in jre 1.6 while it is running in jre 1.4
By Sanjay Dwivedi in forum AWT / SwingReplies: 0Last Post: 08-26-2009, 01:03 PM -
Total noob
By J_Walker in forum New To JavaReplies: 9Last Post: 04-24-2009, 03:10 AM -
Printing total out
By denisdoherty in forum New To JavaReplies: 1Last Post: 04-25-2008, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks