Results 1 to 9 of 9
Thread: Integers and Lists
 12222009, 12:24 PM #1Member
 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
 12222009, 12:28 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
What have you tried yourself sofar? Are those numbers stored in a List? (ArrayList etc.)
kind regards,
Jos
 12222009, 12:31 PM #3Member
 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...
 12222009, 12:36 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
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
 12222009, 12:41 PM #5Member
 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?)
 12222009, 01:03 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
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; }
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
Jos
 12222009, 01:10 PM #7Member
 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...
 12222009, 01:15 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
 01272010, 10:49 AM #9Member
 Join Date
 Jan 2010
 Posts
 23
 Rep Power
 0
Similar Threads

Sorting 3 Integers Using If Else
By MSteinman in forum New To JavaReplies: 12Last Post: 02192010, 01:52 PM 
how to get the Integers out of a String
By JordashTalon in forum New To JavaReplies: 10Last Post: 01302009, 07:28 PM 
How to ask for several integers from the program user?
By busdude in forum New To JavaReplies: 1Last Post: 10202008, 07:55 PM 
Finding Median of X Integers
By Hasan in forum New To JavaReplies: 3Last Post: 08122008, 02:06 PM 
Random Integers
By www.kwalski.com in forum Java AppletsReplies: 8Last Post: 12092007, 06:49 PM
Bookmarks