# Thread: Integers and Lists

1. Member
Join Date
Nov 2009
Posts
32
Rep Power
0

## Integers and Lists

Hello,

I am stuck at some point in Java.
I have a list of 100 numbers that I have to check for the average of the 100 numbers.
The point is that they are listed in a list beneath eachother, the example is under...
My question is how to tally up these numbers, if tey are in a textfield like this:
Java Code:
```73
37
27
17
29
30
87
50
1
3
39```

2. What have you tried yourself sofar? Are those numbers stored in a List? (ArrayList etc.)

kind regards,

Jos

3. Member
Join Date
Nov 2009
Posts
32
Rep Power
0
Well, I load the list from a .txt file, and then these numbers come in a textfield.
Then I have to calculate from these numbers the average...

4. Originally Posted by TGH
Well, I load the list from a .txt file, and then these numbers come in a textfield.
Then I have to calculate from these numbers the average...
Do all those numbers show up in one single textfield? Why not compute the sum (and count the number of numbers) while loading them from your text file? You already have the loop for reading the numbers from the file; just add a few statements and you're done.

kind regards,

Jos

5. Member
Join Date
Nov 2009
Posts
32
Rep Power
0
Sorry, I dont know what you mean by that. I doenst understand how to compute the sum...?
I also know that there are 100 numbers..., I have to calculate the average of those 100 numbers...!

(misschien kan je in het Nederlands een PM sturen?)

6. Originally Posted by TGH
Sorry, I dont know what you mean by that. I doenst understand how to compute the sum...?
I also know that there are 100 numbers..., I have to calculate the average of those 100 numbers...!

(misschien kan je in het Nederlands een PM sturen?)
You write here, you read here, it's a forum after all; you must have a loop somewhere in your code where you read the numbers from your file:

Java Code:
```while <a next number to read> {
read number;
stick the number in a list;
}```
You can add a bit to that loop:

Java Code:
```double sum= 0;
int count= 0;

while <a next number to read> {
read number;
count++; // keep track of the number of numbers;
sum+= number; // update the sum
stick the number in a list;
}
double average= 0;
if (count > 0) // did we actually read anything?
average= sum/count; // calculate the average```
kind regards,

Jos

7. Member
Join Date
Nov 2009
Posts
32
Rep Power
0
Yes, Thank you I solved it.
There was a bit of code that had a loop in it.
It read the rules of the incoming textfile, I simple counted that to eachother, and so I / it thru 100...

8. Originally Posted by TGH
Yes, Thank you I solved it.
There was a bit of code that had a loop in it.
It read the rules of the incoming textfile, I simple counted that to eachother, and so I / it thru 100...
Good; problem solved then; b.t.w. don't just assume that the file contains 100 numbers; count them just in case a file pops up that has 99 numbers. You don't want to do your precious calculations with the wrong average. Have fun.

kind regards,

Jos

9. Member
Join Date
Jan 2010
Posts
23
Rep Power
0

## Array Using Queue

Hi there! Can sombody give me sample how to create an array program using Queue? Thank you.

#### Posting Permissions

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