/** * This program analyzes temperature data for Salem, OR, 1928-2005 * The data was downloaded from http://www.nws.noaa.gov/climate/local_data.php?wfo=pqr * The daily temperatures were averaged within each month to give the values in the * data file aveMonthlyTemps.txt */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.Arrays; public class TemperaturesSkeleton { public static int[] year = new int[934]; public static int[] month = new int[934]; public static float[] minTemp = new float[934]; public static float[] maxTemp = new float[934]; public static String[] monthNames = {"","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; public static void main(String[] args) { readFile("aveMonthlyTemps.txt"); printData(); } /** Print out all of the data */ public static void printData() { for (int i=0;i< minTemp.length;i++) { System.out.println(monthNames[month[i]] + " " + year[i] + " " + "ave min/max temps: (" + minTemp[i] + " , " + maxTemp[i] +")"); } } /** Read in the data from the file. Assumes each line of the file, e.g. * 1928 1TX 45.1 // year 1928 January max temp * 1928 1TN 34 // year 1928 January min temp * has the form: * ASCII POSITION DESCRIPTION * 0-3 year (e.g. 1928) * 4-5 month (1, 2, .... 12) * 6 T, indicates temperature (not read) * 7 X or N, indicates maX or miN (each line alternates min and max) * remaining the temperature (float) * Assumes the file is located in the same folder as this java file. * * @param filename the name of the ascii file name */ public static void readFile(String filename) { try { File inputFile = new File(filename); Scanner in = new Scanner(inputFile); int cnt = 0; while(in.hasNextLine()) { String line = in.nextLine(); year[cnt] = Integer.parseInt( line.substring(0,4) ); month[cnt] = Integer.parseInt( line.substring(4,6).trim() ); float temp = Float.parseFloat(line.substring(9).trim()); if (line.charAt(7) =='X') { maxTemp[cnt] = temp; // System.out.println("year " + year[cnt] + " month = " + month[cnt] + " max temp = " + maxTemp[cnt]); } else { minTemp[cnt] = temp; // System.out.println("year " + year[cnt] + " month = " + month[cnt] + " min temp = " + minTemp[cnt]); cnt++; } } System.out.println("count = " + cnt); } catch (FileNotFoundException e) { System.out.println("File not found."); } } }