I'm trying to find the average of "pressure" in the code. However, "pressure" is an array. My main question is: how would I find the average of the numbers in an array?
In the code, there area that contains the array "pressure" is all the way towards the bottom.
import java.util.Scanner; import java.io.File; import java.io.IOException; public class Hurricanes2 { public static void main(String[] args) throws IOException { Scanner inFile = new Scanner(new File("hurcdata2.txt")); int numLines = 0; while(inFile.hasNextLine()) { inFile.nextLine(); numLines++; } inFile.close(); inFile = new Scanner(new File("hurcdata2.txt")); int [] years = new int[numLines]; String [] name = new String [numLines]; int [] category = new int [numLines]; int [] pressure = new int [numLines]; double [] wind = new double [numLines]; String [] month = new String [numLines]; System.out.printf("%45s\n", "Hurricanes 1980 - 2006"); System.out.println(); System.out.println("Year Hurricane Category Pressure (mb) Wind Speed (mph)"); System.out.println("======================================================================="); int i=0; while(inFile.hasNext()) { years[i] = inFile.nextInt(); month[i] = inFile.next(); pressure[i] = inFile.nextInt(); wind[i] = inFile.nextDouble(); name[i] = inFile.next(); i++; } inFile.close(); int cat1 = 0; int totalCat = 0; for(int m = 0; m < wind.length; m++) { wind[m] = wind[m] * 1.15; if(wind[m]>=74 && wind[m]<=95) { category[m] = 1; totalCat++; } else if(wind[m]>=96 && wind[m]<=110) { category[m] = 2; totalCat+=2; } else if(wind[m]>=111 && wind[m]<=130) { category[m] = 3; totalCat+=3; } else if(wind[m]>=131 && wind[m]<=155) { category[m] = 4; totalCat+=4; } else { category[m] = 5; totalCat+=5; } } double avgCategory = (double)totalCat/category.length; int totalPres = 0; for(int p = 0; p < pressure.length; p++) { totalPres++; } double avgPressure = (double)totalPres/pressure.length; for(int n = 0; n < years.length; n++) { System.out.printf("%1d %12s %10d %15d %20.2f%n", years[n], name[n], category[n], pressure[n], wind[n]); } System.out.println("========================================================================"); System.out.print("Average"); System.out.printf("%21.0f %10.0f", avgCategory, avgPressure); } }