I'm currently trying to figure out how to calculate the average temperature for a day. The day usually has 24 measurements, 00:00 01:00... etc. The file contains information divided by ';'. Like this:
2020-12-30;18:00:00;1.9;G
2020-12-30;19:00:00;1.3;G
2020-12-30;20:00:00;1.1;G
2020-12-30;21:00:00;1.0;G
2020-12-30;22:00:00;1.1;G
2020-12-30;23:00:00;1.3;G
2020-12-31;00:00:00;1.1;G
2020-12-31;01:00:00;1.4;G
2020-12-31;02:00:00;1.9;G
2020-12-31;03:00:00;1.9;G
2020-12-31;04:00:00;2.4;G
I managed to store the data in a List of the constructed type Data: This is where I read the file and call the constructor to create an object with the following information, LocalDateTime, Degrees, whether the measurement was approved or not.
public void loadData(String filePath) throws IOException {
List<String> fileData = Files.readAllLines(Paths.get(filePath));
for (String fileDatum : fileData) {
List<String> list = Arrays.asList(fileDatum.split(";"));
LocalDateTime localDateTime = LocalDateTime.of(
LocalDate.parse(list.get(0)), LocalTime.parse(list.get(1)));
double temp = Double.parseDouble(list.get(2));
String kontroll = list.get(3);
Data data = new Data(localDateTime, kontroll, temp);
vaderData.add(data);
}
}
This is where I am trying to calculate the average temperature of a day. I manage to calculate for one day, but don't really understand how I can go on to the next day without having to iterate the entire list again. I also experimented with using the Collectors class but had no luck there neither.
public List<String> averageTemperatures(LocalDate dateFrom,
LocalDate dateTo) {
double sum = 0;
int count = 0;
for (Data average : vaderData) {
if (dateFrom.isEqual(average.getDate().toLocalDate())) {
sum = sum + average.getTemp();
count++;
}
}
/*
Map<LocalDate, Double> Averagetemperature = vaderData.stream()
.filter(Data -> !Data.getDate().toLocalDate().isAfter(dateTo)
&& !Data.getDate().toLocalDate().isBefore(dateFrom))
.collect(Collectors.groupingBy(Data::getDate,
Collectors.averagingDouble(Data::getTemp)));
*/
return null;
}