I need help and guidance in using data from a csv file to be able to calculate average values from the CSV file.I am trying to calculate the meanAverageTemperature in my files. I have so far written my code to read the files and it is working ok but when it comes to working out the average I am not getting any joy. My files are structured in this format
2012,41.9,20.5,30.8,125.4,44.9,118.2,99.3,66.7,55. 1,76.5,91.2,105.8
1798,3.6,4,5.1,10.4,12.9,16.9,16.3,16.4,13.6,9.9,4 .7,1.5
and I need to read data after the first value in the array because array[0] is the year. Here is my code so far
package org.com1027.cw2.mm00422; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Stack; public class Data { private YearlyData[] allYears; //private String precipitationFileName; //private String temperatureFileName; public Data(String precipitationFileName, String temperatureFileName) { super(); this.allYears = new YearlyData[140]; //this.precipitationFileName = precipitationFileName; //this.temperatureFileName = temperatureFileName; this.parseFile(precipitationFileName, temperatureFileName ); } private void parseFile(String precipitationFileName, String temperatureFileName) { BufferedReader word_reader = null; BufferedReader word_reader2 = null; // stack created so that we can use it to ensure that all the readers are closed properly at the end of this method Stack<BufferedReader> stack1 = new Stack<BufferedReader>(); Stack<BufferedReader> stack2 = new Stack<BufferedReader>(); try { // Attempt to open the files word_reader = new BufferedReader(new FileReader(precipitationFileName)); word_reader2 = new BufferedReader(new FileReader(temperatureFileName)); // readers opened so add to stack stack1.push(word_reader); stack2.push(word_reader2); // Read and parse each line from the file // so create a local variable to hold the line String line1 = null; String line2 = null; //here you might need to use indexes for the yearly data array while ((line1 = word_reader.readLine()) != null && (line2 = word_reader2.readLine())!=null ) { //create the local variables to use; // Split the line in a file String[] precipValues = line1.split(","); String[] tempValues= line2.split(","); //Assign first array element to year int year = Integer.parseInt(precipValues[0]); double[] precipitation = new double[12]; double[] temperature = new double[12]; // do any validation checking on the data here before you assign them to code // Make sure we have enough values to parse: i.e. name and 4 years. if (precipValues.length == 13 && tempValues.length == 13 ){ //assign the first array element to the name String precipYear = precipValues[0]; String tempYear = tempValues[0]; // Remaining values of marks are in the order they are in the file for (int i = 1; i < precipValues.length; i++) { // Start from 1. precipitation[i-1] = Double.parseDouble(precipValues[i]); temperature[i-1] = Double.parseDouble(tempValues[i]); //marks[i - 1] = Integer.parseInt(values1[i]); } //StudentMark student = new StudentMark(name,marks); //so this is like creating a yearly data object System.out.println(line1); // You don't need this line, but it's useful to see what's going on when debugging System.out.println(line2); // my commenting this.printTest(student); //put the object you've just created into the yearlydata array as well // here we are not doing anything else with that object. //you will have other methods in your class which need to use the array //of yearly data objects so you need to store the object that you are //creating each time you read in a line in the yearly data array. }//end of if } //end of while } //end of try // buffered reader failure catch (IOException e) { e.printStackTrace(); } finally { // Always close the readers, if they have been opened this.close(stack1); this.close(stack2); } } private void close(Stack<BufferedReader> readers) { BufferedReader currentReader = readers.pop(); if (currentReader != null) { try { currentReader.close(); } catch (IOException e) { // if failure to close e.printStackTrace(); } finally { // if more readers to close then call recursively if (!readers.isEmpty()) { this.close(readers); } } } } public double calculateMeanPrecipitationMonth(Month month){ double sum = 0; for(YearlyData monthPrecipitation : this.allYears){ sum += monthPrecipitation.getPrecipitationMonth(month); } return sum/this.allYears.length; } }