简体   繁体   中英

how to find average of a List java

I have a question on how to find the average of a List. I don't know what is wrong with my code, because I did the same for all the three averages but only the first one works, the others just show 0 , as shown below in my output:

Average:                                  58                                  0                                  0

Below is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Hurricanes2{

public static void main(String args[]) throws FileNotFoundException{

    Scanner scan = new Scanner(new FileReader(new File("/Users/timothylee/hurcdata2.txt")));
    List<Integer> year = new ArrayList<Integer>();
    List<Integer> windspeed = new ArrayList<Integer>();
    List<Integer> pressure = new ArrayList<Integer>();
    List<String> name = new ArrayList<String>();
    List<Integer> category = new ArrayList<Integer>();

     while(scan.hasNext()){
         String input = scan.nextLine();
         String[] hurricane = input.split("\\s+");
         year.add(Integer.parseInt(hurricane[0]));
         windspeed.add(Integer.parseInt(hurricane[2]));
         pressure.add(Integer.parseInt(hurricane[3]));
         name.add(hurricane[4].trim());
         category.add(Integer.parseInt(hurricane[5]));
     }

     int sum = 0;
     for(Integer integer : pressure){
         sum += integer.intValue();
     }

     double average = sum / pressure.size();

     // create menu
     System.out.printf("%50s%n%n", "Hurricanes 1980 - 2006");
     System.out.printf("%1s%20s%20s%20s%20s%n", "Year", "Hurricane", 
             "Category", "Pressure(mb)", "Wind Speed (mph)");
     System.out.println("__________________________________________________"
             + "__________________________________");
     for(int i = 0; i < year.size(); i++){
         System.out.printf("%4d%20s%20d%20d%20d%n", year.get(i), name.get(i)
                 , category.get(i), pressure.get(i), windspeed.get(i));
     }
     System.out.println("__________________________________________________"
             + "__________________________________");
     int averageCategory = 0;
     int averagePressure = 0;
     int averageWindSpeed = 0;
     for(int i = 0; i < category.size(); i++){
         averageCategory = i / category.get(i);
         averagePressure = i / pressure.get(i);
         averageWindSpeed = i / windspeed.get(i);
     }
     System.out.printf("%1s%35d%35d%35d%n", "Average: ", averageCategory, 
             averagePressure, averageWindSpeed);
}
}

How can I fix this? Any help will be greatly appreciated.

I can't see your input, but....

I don't think your for loop is doing what you want it to. Each iteration, it is completely overwriting what is in averageCategory, averagePressure, averageWindspeed with whatever is on the right side of the equals sign.

Is this is what you meant to do? Usually in calculating averages we get a sum and then divide once by the total number of items.

You are doing wrong here:

 double average = sum / pressure.size();

In this way you are doing integer division, which is not good when computing average, expecially if the values are near to 1. You need to cast it:

 double average = (double)sum / (double)pressure.size();

also you need to cast here:

for(int i = 0; i < category.size(); i++){
         averageCategory = (int) ((double)i / (double)category.get(i));
         averagePressure =(int) ((double) i / (double)pressure.get(i));
         averageWindSpeed = (int) ((double)i / (double)windspeed.get(i));
     }

Also, this last piece of code doesn't seem to calculate any average. You should calculate the sum and divide it for the size of data. But remember to cast integer division to double as I shown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM