/* * It’s been raining for a week around here so we’re going to find some * information about the recent rainfall from rainfall measurement data * stored in a file. The file contains measurements of rainfall in * different cities on different days. * monthNum dayNum cityName rainfall1 rainfall2 rainfall3 … rainfall6 * We want our program to use the file data to find the daily total rainfall * for a city, the average amount of rainfall per hour, and then an average * rainfall amount per day across all the cities recorded. */ package rainfalloct27; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class RainfallOct27 { /** * @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 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 = 1; // different numbers of cities on different days, int dayCount = 1; // there are an unknown number of days, and int msmtCount = 0; // number of rainfall measurements in file File inFile = new File("RainfallData.txt"); // Input in a file containing Scanner input; try { input = new Scanner(inFile); // Will have to use try-catch here with keyboard input to end and dayCount set to 10 } catch (FileNotFoundException fnf) { input = new Scanner(System.in); dayCount = 10; } System.out.printf("Welcome to the Rainfall Data Aggregator (using arrays)\n\n"); //Print hello message String inString; // to read one line of file Scanner inLine; // to read from string while (input.hasNext() && (dayCount < 8))//Loop while lines in file // unknown number of lines in the file. ; multiple lines in the file. { //and dayCount < 8 // There are no more than seven days worth of data in the file. lineCount++; //Increment lineCount inString = input.nextLine(); //Scan from File to get one line as inString // 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 inLine = new Scanner(inString); //Scan from inString to get elements //Each line of the file will have the form of ; Every line will have this format. //monthNum dayNum cityName rainfall1 rainfall2 rainfall3 … rainfall6 //10 17 Arlington 0.0 0.7 0.3 monthNum = inLine.nextInt(); //Read int for monthNum // month as number dayNum = inLine.nextInt(); //Read int for dayNum // day as number //If prevMonth isn’t zero and prevMonth is different from monthName or //[prevDay isn’t zero and] prevDay isn’t same as dayName if (((prevMonth != 0) && (prevMonth != monthNum)) || ((prevDay != 0) && (prevDay != dayNum))) { // Find the daily average and print results avgPerDay = avgNPrint(cityCount, prevMonth, prevDay, avgPerDay); //Set cityCount to 0 and Increment dayCount // there are an unknown number of days, cityCount = 0; dayCount++; avgPerDay = 0; } cityName = inLine.next(); //Read .next for cityName and increment cityCount // one word names 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. ; at least three and no more than six // If string has a double, read rain4, 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 rain5, add it to dailyTotalCity, and msmtCount++ rain[4] = inLine.nextDouble(); dailyTotalCity += rain[4]; msmtCount++; if (inLine.hasNextDouble()) { // Then If string has a double, read rain6, add it to total, and msmtCount++ rain[5] = inLine.nextDouble(); dailyTotalCity += rain[5]; msmtCount++; } } } avgPerHourCity = dailyTotalCity / msmtCount; // avgPerHourCity gets average of all msmts //dailyTotalCity over msmtCount avgPerDay += dailyTotalCity; // Add dailyTotalCity to avgPerDay // 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 \n", monthNum, dayNum,cityName, dailyTotalCity, avgPerHourCity ); 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 "); prevMonth = monthNum; //Set prevMonth to monthName prevDay = dayNum; //Set prevDay to dayName dailyTotalCity = 0; //Set dailyTotalCity to 0 msmtCount = 0; } //End of loop for lines in file // Find the daily average and print results avgPerDay = avgNPrint(cityCount, prevMonth, prevDay, avgPerDay); //Print number of lines in file and thank you message System.out.printf("\nThere were %d lines in your input file. \nThank you for testing the Rainfall Data Aggregator.\n", lineCount); } public static double avgNPrint(int cityCount, int prevMonth, int prevDay, double avgPerDay) { //then Find average for the day // avgPerDay / dayCount avgPerDay = avgPerDay / cityCount; //Print average for the day in format: // "The average rainfall across the area on 10/17 was 7.18" // 1 line of output per line of input plus 1 line of input for each day System.out.printf("The average rainfall across the area (%1d cities) on %2d/%2d was %4.1fin \n\n", cityCount, prevMonth, prevDay, avgPerDay); return avgPerDay; } }