Results 1 to 9 of 9
Thread: Integers and Lists
- 12-22-2009, 11:24 AM #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
- 12-22-2009, 11:28 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
What have you tried yourself sofar? Are those numbers stored in a List? (ArrayList etc.)
kind regards,
Jos
- 12-22-2009, 11:31 AM #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...
- 12-22-2009, 11:36 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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, 11:41 AM #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?)
- 12-22-2009, 12:03 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
You can add a bit to that loop:Java Code:while <a next number to read> { read number; stick the number in a list; }
kind regards,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
- 12-22-2009, 12:10 PM #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...
- 12-22-2009, 12:15 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 01-27-2010, 09:49 AM #9
Member
- 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: 02-19-2010, 12:52 PM -
how to get the Integers out of a String
By JordashTalon in forum New To JavaReplies: 10Last Post: 01-30-2009, 06:28 PM -
How to ask for several integers from the program user?
By busdude in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:55 PM -
Finding Median of X Integers
By Hasan in forum New To JavaReplies: 3Last Post: 08-12-2008, 02:06 PM -
Random Integers
By www.kwalski.com in forum Java AppletsReplies: 8Last Post: 12-09-2007, 05:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks