My question is how do I calculate the moving average of the 7 most recent values using ArrayList after the first seven values?
This is my code for the first seven values, what I fail to understand is how I would get the average of the 7 most recent values after the first seven values, I would really appreciate it if anyone could help me with this.
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
public class MovingAverage{
public static void main(String[]args) throws FileNotFoundException{
int sum = 0;
Scanner s = new Scanner(new File("values.txt"));
ArrayList<Integer> values = new ArrayList<>();
ArrayList<Integer> averages = new ArrayList<>();
System.out.println("\tValue"+"\t\tRolling Average");
System.out.println();
while(s.hasNext())
{
values.add(s.nextInt());
}
for(int i = 0; i<7;i++)
{
sum = sum + values.get(i);
averages.add(sum/(i+1));
System.out.println("\t"+values.get(i)+"\t\t"+averages.get(i));
}
}
}