Results 1 to 20 of 21
Thread: Finding totals  HELP!!
 11192010, 09:40 AM #1Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
Finding totals  HELP!!
I am having a problem with finding the total of an integer array given certain conditions of the array.
I have a program that writes integers to a text file and adds new values to the same text file each time it is run. For example, the first run of the program leads to a text file being produced with one integer value in. The next program run adds another integer to the end of the same text file and so on. What I want to do is write a piece of code that determines when the total of the integers in the text file reaches a certain value, say 10. However, the total must come from consecutive values in the file. So, for example, if the program generates the numbers 1,2,2,1,3,1 then a total of 10 would be flagged. However, if the program generates the numbers 1,0,2,2,1,3,1 then it wouldn't flag anything as the total of consecutive values is only 9. I thought about maybe trying to incorporate a counter that increases by 1 if it comes across a 1, increases by 2 if it comes across a 2 etc., and resets back to zero if it comes across a zero but I'm not sure how to go about doing that.
Does anybody have any ideas of the best way to tackle this??
Many thanks in advance
 11192010, 09:51 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,655
 Blog Entries
 7
 Rep Power
 21
 11192010, 10:09 AM #3Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
OK let me try and explain...
The values in the text file represent scores. I want the program to flag when a total score of 10 is reached. So for example, if the values in the text file were 2,2,2,2,2 the the program would flag up a total score of 10 has been reached. If however, the values were 2,2,2,0,2,2 then it wouldn't flag because, although the total is actually 10, it is not from numbers in a continuous run. So for this set of values (2,2,2,0,2,2) the running total is 6 and then 4. In other words, if the value is zero then the running total goes back to zero. Hope that makes a bit more sense and many thanks for your help.
Mike
 11192010, 10:41 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,655
 Blog Entries
 7
 Rep Power
 21
Ah, got it; thanks for the explanation. So for every number read from the file you have a value 'total' containing the total value of the sequence of numbers and you have a value 'number' containing the current value. For every number read you have to run this:
Java Code:if (number == 0) total= 0; else { total+= number; if (total >= 10) flag_it(); }
Joscenosillicaphobia: the fear for an empty beer glass
 11192010, 10:43 AM #5Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
Ah OK that makes sense now. Have been trying to figure that out for days so thank you very much, really appreciate it! I'll give it a go.
Thanks again
Mike
 11192010, 12:02 PM #6Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
.................
Last edited by mikeg; 11192010 at 12:05 PM.
 11192010, 12:04 PM #7Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
Sorry not sure what happened there....
I have written the code below but it doesn't seem to work. It just returns a total of 0 everytime? Is there anything wrong or missing here?
Java Code:int Total = 0; int number; number = Scores[rows]; for(rows=0; rows<Scores.length; rows++){ if(number==0) Total=0; else{ Total = Total + number; } }
 11192010, 01:57 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,655
 Blog Entries
 7
 Rep Power
 21
cenosillicaphobia: the fear for an empty beer glass
 11192010, 03:20 PM #9Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
Sorry, I should have said, that was only part of my code. Everything else is working fine apart from the totalling bit. The old code I was using is shown below and worked fine but this obviously just totalled the whole array.
Java Code:int Total = 0; for(rows=0; rows<Scores.length; rows++){ Total = Total + Scores[rows];} System.out.println("Total is "+Total);
Java Code:int Total = 0; int number; number = Scores[rows]; for(rows=0; rows<Scores.length; rows++){ if(number==0) Total=0; else{ Total = Total + number; } }
 11192010, 03:31 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,655
 Blog Entries
 7
 Rep Power
 21
cenosillicaphobia: the fear for an empty beer glass
 11192010, 03:40 PM #11Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
Yes, tried that aswell but still returns a total of zero everytime no matter what values are in the text file?
Java Code:int Total = 0; int number; for(rows=0; rows<Scores.length; rows++){ number = Scores[rows]; if(number==0) Total=0; else{ Total = Total + number; } }
 11192010, 04:09 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,655
 Blog Entries
 7
 Rep Power
 21
 11192010, 04:19 PM #13Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
Hi, yeah checked that and the elements in the array are as per the text file?
 11192010, 04:32 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,655
 Blog Entries
 7
 Rep Power
 21
 11192010, 04:33 PM #15Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
This is the code that reads in the text file of scores and calculates the total. Maybe an error somewhere in this part? Thank you for this!!
Java Code:int [] Scores = new int [20000]; File scores = new File("Scores.txt"); int rows = 0; BufferedReader bufRdrs = new BufferedReader(new FileReader(scores)); String lines = null; //read each line of text file while((lines = bufRdrs.readLine()) != null && rows < Scores.length) { StringTokenizer sts = new StringTokenizer(lines,","); while (sts.hasMoreTokens()) { //get next token and store it in the array Scores[rows] = Integer.parseInt(sts.nextToken()); } rows++; } int Total = 0; int number; for(rows=0; rows<Scores.length; rows++){ number = Scores[rows]; if(number==0) Total=0; else{ Total = Total + number; } }
 11192010, 04:39 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,655
 Blog Entries
 7
 Rep Power
 21
 11192010, 04:42 PM #17Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
Ah ok, makes sense. Is there any way around that? Obviously I can't set the size of the array then. Can I have a dynamic array or is there any way I can get it to ignore any null values?
thanks again
 11192010, 05:01 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,655
 Blog Entries
 7
 Rep Power
 21
You already keep track of the numbers actually read: the row variable equals the total numbers read. In your second loop use the 'rows' variable instead of Scores.length and use another variable as the loop counter. That should do it.
kind regards,
Jos
ps. Alternatively I'd use an ArrayList<Integer> and don't worry about it anymore.cenosillicaphobia: the fear for an empty beer glass
 11192010, 05:04 PM #19Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
OK thank you very much for that. I'll have a go!
Mike
 11192010, 05:34 PM #20Member
 Join Date
 Nov 2010
 Posts
 25
 Rep Power
 0
All sorted!! Just added a counter in to the first loop and then changed rows<Scores.length to rows<counter and it works great!! Very pleased!! I've also written a piece of code the writes a zero to the text file when a total of 10 is reached. This then resets the total back to zero.
Thank you once again for all your help. Have a good weekend!!
Mike
Similar Threads

Running totals in java. HELP!
By mikeg in forum New To JavaReplies: 8Last Post: 11192010, 09:29 AM 
finding max value
By rocky86 in forum New To JavaReplies: 6Last Post: 10102010, 01:37 AM 
Finding the largest value.
By Blacky777 in forum New To JavaReplies: 7Last Post: 02022010, 08:52 PM 
Java Loops  accumulating totals
By iv3java in forum New To JavaReplies: 6Last Post: 12152009, 05:39 AM 
Finding where to draw
By flok in forum AWT / SwingReplies: 4Last Post: 11242009, 05:55 PM
Bookmarks