The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Problem with executing data from CSV file

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

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;
}
}

Re: Problem with executing data from CSV file

I need help with how after I have read the csv files I can create a method that will allow me to calculate averages, totals or get the lowest values. So far my code is just reading the file but my methods for calculating the average temperatures line by line are not working. In my code I have put in my method for reading the files and right at the bottom is the method I am trying to use for calculating the Average values from the csv files. I think I might need to create a 2D array because my csv files start with the year first and then the values and I need to calculate the files for year line by line.

Re: Problem with executing data from CSV file

If you look at the code I posted in the first forum you will see that I have already created a method that allows me to loop through the files line by line and create an array and that is working fine. My problem is my method for calculating the average values line by line is the one that is not working and thats where I need a bit of guidance.

Re: Problem with executing data from CSV file

method for calculating the average values line by line

Are the values in an array? Normally values in arrays are processed using a loop.

What array is the data in? What code have you written to process the data in that array?
If the data read methods have been tested, you could put them aside and move to working on the code to process the data in the array(s). Define some arrays with a small number of elements for testing.