- #1
SalfordPhysics
- 69
- 1
1. Problem: The task is to import an excel (.csv) file containing monthly weather data and produce a programme that can execute different averages on a yearly basis. i.e.;, produce average rainfall, maximum or minimum temperatures for desired years.
I have thus far managed to calculate average values for entire rows, but I want the programme to only calculate averages of desired values (yearly basis), so that this year's average data can be requested.
2. Relevant Information:
The format of the excel file is (representing a column each): {Year, Month, Max Temp, Min Temp, Rainfall, ...}
In the attempt below, avMonth is showing all the months listed then the average at the bottom as I expected. I have no idea where to go now to complete the task. 3. Attempt:
Mod note: Added [ code ] tags to restore indentation.
I have thus far managed to calculate average values for entire rows, but I want the programme to only calculate averages of desired values (yearly basis), so that this year's average data can be requested.
2. Relevant Information:
The format of the excel file is (representing a column each): {Year, Month, Max Temp, Min Temp, Rainfall, ...}
In the attempt below, avMonth is showing all the months listed then the average at the bottom as I expected. I have no idea where to go now to complete the task. 3. Attempt:
Mod note: Added [ code ] tags to restore indentation.
Code:
package weatherPackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String fileName = "sheffielddata.csv";
File file = new File(fileName);
try {
Scanner inputStream = new Scanner(file);
double sum = 0;
double numberofMonths = 0;
while (inputStream.hasNext()){
String data = inputStream.next();
String[] values = data.split(",");
double avMonth = Double.parseDouble(values [1]);
sum += avMonth;
numberofMonths++;
System.out.println(avMonth);
}
inputStream.close();
System.out.println("Average - " + (sum / numberofMonths));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Last edited by a moderator: