/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package rainfallextrastep; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class RainfallExtraStep { /** * @param args the command line arguments */ public static void main(String[] args) { int monthNum, dayNum; //rainfall in different cities on different days. String cityName; //rainfall in different cities //double rain1, rain2, rain3, rain4, rain5, rain6; // inches and fractions (ex. 2.3”) Double[] rain = new Double[6]; int prevMonth = 0, prevDay = 0; double dailyTotalCity = 0; // find the daily total rainfall for a city double avgPerHourCity = 0; // know the average amount of rainfall per hour for a city double dailyTotalDay = 0; double avgPerDay = 0; // find an average rainfall amount per day across all the cities int lineCount = 0; // There will be multiple lines in the file. int cityCount = 0; // different numbers of cities on different days, int dayCount = 0; // there are an unknown number of days, and int msmtCount = 0; // number of rainfall measurements in file int totMsmtCount = 0; File inFile = new File("RainfallData.txt"); // Input in a file containing //monthNum dayNum cityName rainfall1 rainfall2 rainfall3 … rainfall6 //10 17 Arlington 0.0 0.7 0.3 // The lines of data in the file are ordered by date from earliest to latest monthNum dayNum // and then within a single date, the lines are ordered by city name. cityName // This info is not used for this program, except to rely on sorted dates in file data for date count Scanner input; System.out.printf("Welcome to the Rainfall Data Aggregator (Extra Step using arrays)\n\n"); //Print hello message try { input = new Scanner(inFile); } catch (FileNotFoundException fnf) { input = new Scanner(System.in); dayCount = 10; System.out.printf("No input data is available\n"); } String inString; // to read one line of file Scanner inLine; // to read from string while ((dayCount < 8) && input.hasNext() )//Loop while lines in file { //and dayCount < 8 // There are no more than seven days worth of data in the file. lineCount++; inString = input.nextLine(); inLine = new Scanner(inString); //Scan from inString to get elements monthNum = inLine.nextInt(); // month as number dayNum = inLine.nextInt(); // day as number // If it isn't the start of the program (prevMonth != 0) AND // the month has changed on this line from last (prevMonth is different from monthName) // OR // If it isn't the start of the program (prevDay != 0) AND // the day has changed on this line from last (prevDay isn’t same as dayName) // THEN // do the calculations for totaling one single day if (((prevMonth != 0) && (prevMonth != monthNum)) || ((prevDay != 0) && (prevDay != dayNum))) { // Find the daily average and print results avgPerDay = avgNPrint(cityCount, prevMonth, prevDay, dailyTotalDay, totMsmtCount); //Reset for reading data for a new date cityCount = 0; dayCount++; avgPerDay = 0; dailyTotalDay = 0; totMsmtCount = 0; } cityName = inLine.next(); //Read .next for one word cityName and increase the count of the cities for this day cityCount++; // There are different numbers of cities on different days, rain[0] = inLine.nextDouble(); // Read double for rain1, rain2, rain3 rain[1] = inLine.nextDouble(); rain[2] = inLine.nextDouble(); dailyTotalCity = rain[1] + rain[2] + rain[0]; // dailyTotalCity gets sum of rain1, rain2, rain3 msmtCount = 3; //msmtCount gets set to 3 // There will be different numbers of rainfall measurements on different lines w min = three and max = six // If string has a double, read rain[3], add it to dailyTotalCity, and increment msmtCount if (inLine.hasNextDouble()) { rain[3] = inLine.nextDouble(); dailyTotalCity += rain[3]; msmtCount++; if (inLine.hasNextDouble()) { // Then If string has a double, read rain, add it to dailyTotalCity, and msmtCount++ rain[4] = inLine.nextDouble(); dailyTotalCity += rain[4]; msmtCount++; if (inLine.hasNextDouble()) { // Then If string has a double, read rain, add it to total, and msmtCount++ rain[5] = inLine.nextDouble(); dailyTotalCity += rain[5]; msmtCount++; } } } avgPerHourCity = dailyTotalCity / (double) msmtCount; // avgPerHourCity gets average of all msmts //dailyTotalCity over msmtCount dailyTotalDay += dailyTotalCity; // sum of all the daily totals // Print daily total and average per hour in the city using format: // On 10/17 in Arlington the total rainfall was 1.0” with an average of 0.3” per hour System.out.printf("On %2d/%2d in %16s the measured rainfall was %4.1fin with an average of %4.1fin per hour for %d hours\n", monthNum, dayNum,cityName, dailyTotalCity, avgPerHourCity, msmtCount ); System.out.printf("%28s %s"," ","Rain was measured as: "); for (int i=0; i < msmtCount;i++) { System.out.printf("%6.2f",rain[i]); System.out.printf(((i == (msmtCount - 1)) ? " in inches.\n\n" : ", ") ); } //System.out.printf("\n "); // reset for reading next line of data prevMonth = monthNum; //Set prevMonth to monthName prevDay = dayNum; //Set prevDay to dayName dailyTotalCity = 0; //Set dailyTotalCity to 0 totMsmtCount += msmtCount; msmtCount = 0; } //End of loop for lines in file // Find the daily average and print results if (dayCount < 10) { avgPerDay = avgNPrint(cityCount, prevMonth, prevDay, dailyTotalDay, totMsmtCount); //Print number of lines in file and thank you message System.out.printf("\nThere were %d lines in your input file. ",lineCount); } //Print thank you message System.out.printf("\nThank you for testing the Rainfall Data Aggregator.\n"); } public static double avgNPrint(int cityCount, int prevMonth, int prevDay, double dailyTotalDay, int totMsmtCount) { double avgPerDay; double avgNumMsmts; double avgPerHour; // Find average for the day // find avg per hour for all cities (dailyTot / totmsmtcnt) and then multi by avg num msmtper day (totmsmt/citycount) System.out.printf("For the area there were %d rainfall measurements made across %d cities\n", totMsmtCount, cityCount); avgPerHour = dailyTotalDay / (double) totMsmtCount; avgNumMsmts = totMsmtCount / (double) cityCount; avgPerDay = avgPerHour * avgNumMsmts; //Print average for the day in format: // "The average rainfall across the area on 10/17 was 7.18" System.out.printf("The average total rainfall across the area (%1d cities) on %2d/%2d was %4.1fin over a %4.1f hour period\n\n\n", cityCount, prevMonth, prevDay, avgPerDay, avgNumMsmts); return avgPerDay; } }