Getting the average of a set of numbers, confused by file IO, please help!

What i need to do is get the average of a set of numbers, the problem is that i am reading these numbers in from files in different directories where each file returns a number, and the average for the directory is the numbers for each file in that directory added together and divided by the number of files in that directory.
public static HashMap readDir(File path, HashMap map) throws IOException{
        File files[] = path.listFiles();
        float totalForDirectory = 0;
         int common = 0;
         int count = 0;
                if(files != null && files.length > 0) {
                          for(int i=0; i<files.length; i++) {
                                if(files.isDirectory()) {
readDir(files[i], map);
} else {
//trying to count the number of files in the directory
count++;
totalForDirectory =(total(common, totalF1(), totalF2(files[i])));
readFile(files[i]);
//will contain pathname, and the average
map.put(path.getCanonicalPath(), totalForDirectory);
return map;
In the above code, totalForDirectory contains the number for each file, i want to add together the result of this variable for each file, "file[i]" in the directory, and divide that by the number of files in that directory.
Id appreciate any help
Thanks

What you want, It seems to me, is a recursive routine (for directories) which returns both a total and a filecount. Then these can be totalled at each level (togther with the count for each file).
Probably the best approach is to pass a small object in which the totals are accumulated.
Define a class something like:
public class Accumulator {
   int count;
   int total;
   public double mean() {
      return total / count;
  public void add(Accumulator add) {
     count += add.count;
     total += add.total;
   public void add(int numberFromFile) {
      count++;
     total += numberFromFile();
  }Create an instance of this at the start of your scan directory method, and pass it when you call make the recursion. At the end of the scan the method would use the first add method to add it's local totals to the hight level total.
Obviously you create an Accumulator for the first scan directory call which becomes the grand total.

Similar Messages

  • I just bought a iphone 3gs from my friend. when he gave it to me it was in recovery mode. Now everytime i try to Restore it i get the error 1603. is there any way to fix this ? please help !

    I just bought a iphone 3gs from my friend. when he gave it to me it was in recovery mode. Now everytime i try to Restore it i get the error 1603. is there any way to fix this ? please help !

    iOS: Unable to update or restore
    Are you sure your friend didn't jailbreak it?
    If so, save yourself a lot of time and trouble - throw it in the trash.

  • I want to re-install my Pre-Installed Lion Macbook Pro, but I can't get the Lion OS from App Store with my Apple ID. Please help, thanks!

    Hi, I want to do a clean re-installation of my end 2011 Macbook Pro which comes with pre-installed Lion OS. I go through the installation steps and after App Store verified my Apple ID, it prompted me that I did not purchase the Lion OS. I am very surprise to see this message which my pre-installed Lion Macbook Pro should have it. Please help and advise how can I get the OS? All other bundle softwares were in the purchase record except the Lion OS.

    That was my mistake - sorry - it IS Command-R to boot to the Recovery disk, not Command-D: I either just hit the wrong key or am just now coming awake from my nap.
    Let me see if I can just explain it in a few steps - I don't know of any link (but it would make a good User Tip):
    Create an up-to-the minute back-up of your internal hard drive using an external hard drive and Time Machine.
    Boot to your recovery partion - holding down Command-R on boot.
    Use Disk Utility to erase Macintosh HD - use one GUID partition and format as "Mac OS Extended (Journaled)". Unless you're selling the machine, there's no need to do a secure erase (which can take hours).
    After you erase the disk, re-install Lion from the same Recovery partition.
    After installation is complete, reboot, again holding down the Command-R keys to boot into the Recovery Partition.
    Restore your Time Machine back-up. This could take several hours.
    Reboot after restore and you're back in business.
    That should take care of everything - all you need is an Internet connection.
    Call back if I didn't explain things too well - at least I got the Command-R right this time!
    Clinton

  • HT2534 According your instruction number 1, when I creat account on computer, I have to click iTune Stores first. But, where can I get the iTune Store to click. I cannot find it. Please help me.

    I cannot find iTune Store to start for free download application.

    You are on your computer and not your iPad ?
    On windows with iTunes 11 on it then control-S gets the left-hand sidebar to show, and on a Mac option-command-S does similar. Then select the iTunes Store on that sidebar, click App Store at the top of the store home page, and find a free app, click on its free price, and then click 'create Apple id' on the popup

  • Hi , i didnt get the email for my secuity question , am trying from 2 weeks , please help , thanx

    hi
    i didnt get the answer for the security question

    I spoke with Apple Support this morning and they believe anything MobileMe related is due to the conversion over to iCloud that's taking place.
    As for other email clients, check with your host to see if settings have changed on their end. I use inmotionhosting.com and over the weekend the updated their outgoing requirements so that a password authentication is now needed.
    Hope that helps get some on the right path to resolving their issues!

  • How can I get the black dot wallpaper from my ipad to my iphone. Please help me out

    I would really love to have the black dot wallpaper fro my ipad on my iphone and I cannot find it anywhere. Someone please help me out.

    Why don't you simply do a screen shot of your iPad and then transfer the photo to your iPhone.

  • HT201210 I am getting 3014 error code when i am trying to restore my ipad. I already followed this article but i keep getting the same error message. This has left my ipad unusable.Please help

    How can i restore my ipad when i lost its passcode. I am trying to restore it from DFU mode but getting error 3014code.

    Errors 3000-3999 (3004, 3013, 3014, 3018, 3164, 3194, and so on): Error codes in the 3000 range generally mean that iTunes cannot contact the update server (gs.apple.com) on ports 80 or 443.
    Update to the latest version of iTunes.
    Verify the computer's date and time are accurate.
    Check that your security or firewall software is not interfering with ports 80 or 443, or with the server gs.apple.com.
    Follow Troubleshooting security software. Often, uninstalling third-party security software will resolve these errors.
    An entry in your hosts file may be redirecting requests to gs.apple.com (see "Unable to contact the iOS software update server gs.apple.com" above).
    Internet proxy settings can cause this issue. If you are using a proxy, try without using one.
    Test restoring while connected to a known-good network.
    MY suggestion would be to try to create a new user on your computer, make sure your fire wall is turned off, make sure your using the cable that came with the device, make sure theres nothing else plugged in, such as camera, printer, mouse and keyboard are fine, and make sure your anti virus is turned off

  • Whenever I try to save a file as a PDF, it tells me it can't save because the file is open somewhere else. This is not the case and I have tried it with different files. Please help. You can also call me at:(301)651-9616

    Whenever I try to save a file as a PDF, it tells me that it can't save, because the file is open somewhere else. I don't have anything else open. I have also tried different files and they all say the same thing. Help.

    Given there are so many problems and removed features in Pages 5, why not just continue using Pages '09?
    http://www.freeforum101.com/iworktipsntrick/viewforum.php?f=22&mforum=iworktipsn trick
    http://www.freeforum101.com/iworktipsntrick/viewtopic.php?t=432&mforum=iworktips ntrick
    Peter

  • How can i get the average graph?(DIAdem)

    hi,
    How can i get the average graph(2D-graph) of 10 measures?
    Can someone please help me?
    THX!!!

    Hello Charleen!
    If you want to do it programmatically have a look at the ChnAverage command in the help (see also StatBlockCalc for advanced calculations). Interactive you can use the statistic functions in the ANALYSIS device.
    Matthias
    Matthias Alleweldt
    Project Engineer / Projektingenieur
    Twigeater?  

  • I'm new to Mac and the program/all called Numbers. I'm trying to use both Average and small in the same formula. What's I'm trying to do is take 20 cells, find the 10 lowest numbers, then get the average and after that multiply it by .96

    I'm new to Mac and the program/all called Numbers. I'm trying to use both Average and small in the same formula. What's I'm trying to do is take 20 cells in a column,  find the 10 lowest numbers, then get the average and after that multiply it by .96  I used to use Excel and the formula worked fine in that. Here is my Formula
    =(average(small(H201:H220,{1,2,3,4,5,6,7,8,9,10})))*.96
    This formula worked in Excel and when I converted my spreadsheet over to Numbers, this formula no longer works.
    The best that I have been able to do so far is use small in 10 different cells, then get the average of the 10 cells and finally multiply that average by .96  So instead of using 1 cell, I'm using 12 cells to get my answer.
    This is a formula that I will be using all the time. The next cell would be =(average(small(H202:H221,{1,2,3,4,5,6,7,8,9,10})))*.96
    Hoping I explain myself well enough and that someone can help me.
    Thanks

    You can still do it in one cell but it will be more unruly than the Excel array formula.
    =average(small(H201:H220,1),small(H201:H220,2),small(H201:H220,3),...,small(H201:H220,10))*0.96
    where you would, of course, replace the "..." with the remaining six SMALL functions.

  • Why can't I get the average function work with empty cells?

    Why can't I get the average function work with empty cells?

    The thing that I am having a problem with is that I made a different form that calculates the average no matter how may of the cells have numbers or not and I didn't use an if function.  I have tried with this other form and and it calulates as if all the cells where being used.  I am using the [*] in both instances but it only works in the one I did earlier.

  • How do you get the average of an array bigdecimels

    how do you get the average of an array bigdecimels... I am looping through the array in a for loop
    for (int i = 0; i < prItems.length; i++) {                                                                                                                                                                                                                                                                                                                  

    int sum = 0;
    for (int i = 0; i < prItems.length; i++) {
    sum = sum + prItems;
    int avg = sum/prItems.length;

  • How can you get the dashes to appear inbetween phone numbers

    Is there anyway that I can get the dashes to appear so phone numbers do not look like a big block of numbers.  so instead of 18001234567 is goes 1-800-123-4567.  I looked in options and it does not look like it is possible

    Hi,
    The dashes aren't necessary for the actual call so the Blackberry doesn't waste space (memory) holding the character.
    Make sense?
    Click Accept as Solution for posts that have solved your issue(s)!
    Be sure to click Like! for those who have helped you.
    Install BlackBerry Protect it's a free application designed to help find your lost BlackBerry smartphone, and keep the information on it secure.

  • How to get the kerning value and set it to back use script?

    hi,guys
    I come back again.
    I encountered a kerning problem.
    how to get the kerning value and set it to back use script?
    Thanks very much!

    For both cases, the filename can be found on the FILE.ReceivedFileName Context Property.  You can access this Property in a Pipeline Component or Orchestration and take any action you want, such as apply to a database.
    The value is accessed by: MyReceivedMessage(FILE.ReceivedFileName)
    In the case of a duplicate EDI Interchange, you would use the Failed Message Routing feature to capture the error message with either an Orchestration or Send Port.

  • GETTING THE AVERAGE OF TIME

    Hi,I have the following code which returns day of week name, position and date and time of surgeries.
    SELECT
    DATENAME(WEEKDAY,OPE_START_TIME) AS DOW,
    [OPE_ORDER_IN_SESS_ASC] AS POSITION,
    OPE_START_TIME AS [TIME]
    FROM table1The data looks like thisHow can i group this data so I get the average start time by day of week and position?

    try this
    \declare @table table(c1 varchar(20),c2 int,c3 datetime2(0))
    insert into @table
    values('monday',1,'2015-04-29 02:09:42')
    ,('monday',1,'2015-04-29 02:05:42'),
    ('tuesday',1,'2015-04-29 02:59:42')
    ,('tuesday',2,'2015-04-29 02:55:42')
    ;with cte as (select avg(datepart(second,c3)+((datepart(minute,c3)+(datepart(hour,c3)*60))*60)) as c3
    ,c2,c1 from @table
    group by c1,c2)
    select right('0'+cast(c3/3600 as varchar),2)+':'+right('0'+cast((c3%3600)/60 as varchar),2)
    +':'+right('0'+cast((c3%3600)%60 as varchar),2) as [AvgTime],c2,c1 from cte
    Hope it Helps!!

Maybe you are looking for