I'm a complete Java newbie. Started Last Monday and have never done any programming in any language before. So please bear with me if I find easy things complicated.
I've been given a text file. As shown below:
The First piece of data is time (in seconds past midnight), the second is score (irrelevant), the third is Jump (dont need to know what this means...)
I can happily read this in using this code:
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
public class ReadText {
public static void main(String[] args) throws Exception {
String InputFile="C:\\PracticeSpreads.txt";
ArrayList<String> fileLines=new ArrayList<String>();
FileReader fr;
BufferedReader br;
fr = new FileReader(InputFile);
br = new BufferedReader(fr);
String line;
br.readLine();
while ((line=br.readLine()) != null) {
fileLines.add(line);
//System.out.print(line+"\n");
}
}
}
I skipped the first line because it makes it easier.
Ok basically what I need to do is create a Daily Time weighted Average Jump.
I can get an average Jump but not a time weighted average Jump.
The formula for time weighted average jump is=
For times when a second has more than one Jump, I want to take the "average jump" for that time period.
But I've literally got no idea as I'm not too sure as to how the开发者_运维知识库 loops would arrange themselves.
Hopefully someone can help me please!
After reading your comments bellow and understanding your requirement better, here is a piece of code that does the job. Note that it assumes that the timestamps in each day are sorted, as in your example:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.AbstractMap.SimpleEntry;
public class ReadText {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
try {
String InputFile = "C:\\PracticeSpreads.txt";
br = new BufferedReader(new FileReader(InputFile));
String line;
List<SimpleEntry<Integer, List<Double>>> valuesInDay = new ArrayList<SimpleEntry<Integer, List<Double>>>();
String date = null;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.startsWith("DATE")) {
if (line.equals(date)) // Found day footer
System.out.println("Average for " + date + ": " + calcDayAvg(valuesInDay));
else { // Found a day header
valuesInDay.clear();
date = line;
}
} else { // Found a value entry
Scanner s = new Scanner(line);
int sec = s.nextInt();
s.nextDouble();
double jump = s.nextDouble();
List<Double> jumps;
if (!valuesInDay.isEmpty() && valuesInDay.get(valuesInDay.size() - 1).getKey() == sec) {
// Same time stamp as prev
jumps = valuesInDay.get(valuesInDay.size() - 1).getValue();
}else {
// New time stamp
jumps = new ArrayList<Double>();
valuesInDay.add(new SimpleEntry<Integer, List<Double>>(sec, jumps));
}
jumps.add(jump);
}
}
} finally {
if (br != null)
br.close();
}
}
private static Double calcDayAvg(List<SimpleEntry<Integer, List<Double>>> values) {
if (values.isEmpty())
return null; // No way to calculate for empty set
double min = values.get(0).getKey();
double max = values.get(values.size() - 1).getKey();
double span = max - min;
if (span == 0)
return null; // Division by zero...
double total = 0;
for (int i=0; i < values.size(); i++) {
SimpleEntry<Integer, List<Double>> entry = values.get(i);
int sec = entry.getKey();
double jumpAvg = getJumpAvg(entry.getValue());
int jumpDuration;
if (i == values.size() - 1)
jumpDuration = 1; // last jump has duration of 1 sec
else
jumpDuration = values.get(i + 1).getKey() - sec;
total += jumpAvg * jumpDuration;
}
return total / span;
}
private static double getJumpAvg(List<Double> jumps) {
double total = 0;
for (Double jump : jumps) {
total += jump;
}
return total / jumps.size();
}
}
精彩评论