# Thread: Finding totals - HELP!!

1. Member
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??

2. Originally Posted by mikeg
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??

kind regards,

Jos

3. Member
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

4. Originally Posted by mikeg
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.
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();
}```
kind regards,

Jos

5. Member
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

6. Member
Join Date
Nov 2010
Posts
25
Rep Power
0
.................
Last edited by mikeg; 11-19-2010 at 01:05 PM.

7. Member
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;
}

}```

8. Originally Posted by mikeg
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;
}

}```
What is the length of the Scores array? What is the value or 'rows' before the loop has started? How come your indentation is so messed up? Don't you have to update the 'number' variable in each pass of the loop?

kind regards,

Jos

9. Member
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);```
I then changed it to this but now I just get a total of zero returned everytime.

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;
}

}```
Any ideas?? I am very new to java by the way so sorry if it's completely wrong.

10. Originally Posted by mikeg
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);```
I then changed it to this but now I just get a total of zero returned everytime.

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;
}

}```
Any ideas?? I am very new to java by the way so sorry if it's completely wrong.
Yup, you should update 'number' in the body of your loop because it is supposed to be equal to the current rows array element, i.e. number= Scores[rows] doesn't belong above the loop but at the start of the body of the loop.

kind regards,

Jos

11. Member
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;
}

}```

12. Originally Posted by mikeg
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;
}

}```
That code snippet is correct; print out the content (each element) of your array and see what's in it.

kind regards,

Jos

13. Member
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?

14. Originally Posted by mikeg
Hi, yeah checked that and the elements in the array are as per the text file?
You have to show us a bit more of the (relevant) code; now we can only guess what's happening. The little code snippet in your previous reply is definitely correct.

kind regards,

Jos

15. Member
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;

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;
}

}```

16. Originally Posted by mikeg
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!!
If your file has less than 20000 numbers in it the last elements of your array will still be 0 (no value is assigned to it) and your Total variable will always end up being 0 as well.

kind regards,

Jos

17. Member
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

18. Originally Posted by mikeg
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
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.

19. Member
Join Date
Nov 2010
Posts
25
Rep Power
0
OK thank you very much for that. I'll have a go!

Mike

20. Member
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

Page 1 of 2 12 Last

#### Posting Permissions

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