Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to find average of a List java
    primarykey
    data
    text
    <p>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 <code>0</code>, as shown below in my output:</p> <pre><code>Average: 58 0 0 </code></pre> <p>Below is my code:</p> <pre><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&lt;Integer&gt; year = new ArrayList&lt;Integer&gt;(); List&lt;Integer&gt; windspeed = new ArrayList&lt;Integer&gt;(); List&lt;Integer&gt; pressure = new ArrayList&lt;Integer&gt;(); List&lt;String&gt; name = new ArrayList&lt;String&gt;(); List&lt;Integer&gt; category = new ArrayList&lt;Integer&gt;(); 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 &lt; 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 &lt; 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); } } </code></pre> <p>How can I fix this? Any help will be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload