Results 1 to 9 of 9
  1. #1
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

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

    kind regards,

    Jos

  3. #3
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    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. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TGH View Post
    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. #5
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    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. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TGH View Post
    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. #7
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    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. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TGH View Post
    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. #9
    rsjava24 is offline Member
    Join Date
    Jan 2010
    Posts
    23
    Rep Power
    0

    Default Array Using Queue

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

Similar Threads

  1. Sorting 3 Integers Using If Else
    By MSteinman in forum New To Java
    Replies: 12
    Last Post: 02-19-2010, 12:52 PM
  2. how to get the Integers out of a String
    By JordashTalon in forum New To Java
    Replies: 10
    Last Post: 01-30-2009, 06:28 PM
  3. Replies: 1
    Last Post: 10-20-2008, 07:55 PM
  4. Finding Median of X Integers
    By Hasan in forum New To Java
    Replies: 3
    Last Post: 08-12-2008, 02:06 PM
  5. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 05:49 PM

Posting Permissions

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