# Integers and Lists

• 12-22-2009, 12:24 PM
TGH
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:
Code:

```73 37 27 17 29 30 87 50 1 3 39```
• 12-22-2009, 12:28 PM
JosAH
What have you tried yourself sofar? Are those numbers stored in a List? (ArrayList etc.)

kind regards,

Jos
• 12-22-2009, 12:31 PM
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...
• 12-22-2009, 12:36 PM
JosAH
Quote:

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
• 12-22-2009, 12:41 PM
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?)
• 12-22-2009, 01:03 PM
JosAH
Quote:

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:

Code:

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

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
• 12-22-2009, 01:10 PM
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...
• 12-22-2009, 01:15 PM
JosAH
Quote:

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
• 01-27-2010, 10:49 AM
rsjava24
Array Using Queue
Hi there! Can sombody give me sample how to create an array program using Queue? Thank you.