// Arup Guha
// 4/10/2026
// Program to illustrate file input - Weather Data

import java.util.*;
import java.io.*;

public class weather {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		System.out.println("Please enter your weather file.");
		String filename = stdin.next();
		Scanner fin = null;
		
		// So we get a valid file.
		while (true) {
		
			// Try opening the file.
			try {
				fin = new Scanner(new File(filename));
			}
			catch (FileNotFoundException e) {
				System.out.println("Sorry, that file wasn't found. Try again.");
				filename = stdin.next();
			}
			
			// We're good.
			if (fin != null) break;
		}
		
		// Create the object, close the file.
		city mine = new city(fin);
		fin.close();
		
		// Print it!
		System.out.println(mine);
	}
}

class city {

	final private static int TOTALYEARS = 2027;
	final private static int NUMMONTHS = 12;
	
	private double[] yearSum;
	private int[] yearNumReadings;
	private double[] monthSum;
	private int[] monthNumReadings;
	
	// Constructor must be given a Scanner object pointing to the beginning
	// of a file with the appropriate format.
	public city(Scanner fin) {
		
		// Using indexes as actual years and months.
		yearSum = new double[TOTALYEARS];
		yearNumReadings = new int[TOTALYEARS];
		monthSum = new double[NUMMONTHS+1];
		monthNumReadings = new int[NUMMONTHS+1];
		
		// Go through all lines.
		while (fin.hasNextLine()) {
			
			// Get the next line and tokenize it.
			StringTokenizer tok = new StringTokenizer(fin.nextLine());
			
			// Get everything from the tokenizer.
			int month = Integer.parseInt(tok.nextToken());
			int day = Integer.parseInt(tok.nextToken());
			int year = Integer.parseInt(tok.nextToken());
			double temp = Double.parseDouble(tok.nextToken());
			
			// Update our four accumulators.
			yearSum[year] += temp;
			monthSum[month] += temp;
			yearNumReadings[year]++;
			monthNumReadings[month]++;
		}
	}
	
	// Returns a string representation of our object.
	public String toString() {
	
		// This is what our chart will have in the beginning.
		String chart = "Year\tAvg Temp\n";
		for (int i=0; i<yearSum.length; i++) {
		
			// Skip.
			if (yearNumReadings[i] == 0) continue;
		
			// Add this row.
			chart = chart + i + "\t" + (yearSum[i]/yearNumReadings[i]) + "\n";
		}
		
		// Put a bit of separation.
		chart = chart + "\n\n";
		chart = chart + "Month\tAvg Temp\n";
		
		// Now go through the months.
		for (int i=1; i<=NUMMONTHS; i++) {
			
			// Skip.
			if (monthNumReadings[i] == 0) continue;
			
			// Add this row.
			chart = chart + i + "\t" + (monthSum[i]/monthNumReadings[i]) + "\n";
		}
		
		return chart;
	}
	
}