Rainfall Statistics Write a program that will input rainfall amounts for each mo
ID: 3832854 • Letter: R
Question
Rainfall Statistics
Write a program that will input rainfall amounts for each month in a year from a file. Get the filename from the user. Use an array to store the input from the file. Print out a table showing the rainfall amount for each of the months in the year, with the month name and the rainfall amount, as well as how much above or below average the rainfall was for that month.
Sample Output:
Enter the filename: rainfall2016.txt
Monthly Rainfall Amounts
for the year 2016
Merced County
Average for year: 1.44
-------------------------------
Month Rain Deviation
-------------------------------
JAN 5.19 3.75
FEB 0.36 -1.08
MAR 4.28 2.84
APR 3.10 1.66
MAY 0.04 -1.40
JUN 0.00 -1.44
JUL 0.00 -1.44
AUG 0.00 -1.44
SEP 0.00 -1.44
OCT 0.32 -1.12
NOV 1.13 -0.31
DEC 2.82 1.38
Use Java!!!!
Explanation / Answer
rainfallStats.txt(Save this file under D Drive.Then the path of the file pointing to it is D: ainfallStats.txt )
5.19
0.36
4.28
3.10
0.04
0.00
0.00
0.00
0.00
0.32
1.13
2.82
____________________
RainfallStatistics.java
import java.io.File;
import java.util.Scanner;
public class RainfallStatistics {
public static void main(String[] args) throws Exception {
// Declaring variables
String fileName;
double sum = 0;
double avg = 0.0;
// creating an double array to hold rainfall stats
double rainfallArr[] = new double[12];
Scanner sc = null;
sc = new Scanner(System.in);
// Sting array which holds names of months
String months[] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL",
"AUG", "SEP", "OCT", "NOV", "DEC" };
// Getting the filename entered by the user
System.out.print("Enter the Filename :");
fileName = sc.nextLine();
// Opening the file
sc = new Scanner(new File(fileName));
int i = 0;
// getting the data from the file and populating those values into array
while (sc.hasNext()) {
rainfallArr[i] = sc.nextDouble();
sum += rainfallArr[i];
i++;
}
sc.close();
// calculating the average
avg = sum / i;
// Displaying the result
System.out.printf("Average for year :%.2f ", avg);
System.out.println("Monthly Rainfall Amounts for the year 2016");
System.out.printf("Merced Country Average for the year :%.2 f", avg);
System.out.println("-------------------");
System.out.println("Month Rain Deviation");
System.out.println("-------------------");
for (int k = 0; k < i; k++) {
System.out.printf("%s %.2f %.2f ", months[k], rainfallArr[k],
(rainfallArr[k] - avg));
}
}
}
______________________
Output:
Enter the Filename :D: ainfallStats.txt
Average for year :1.44
Monthly Rainfall Amounts for the year 2016
Merced Country Average for the year :1.44
-------------------
Month Rain Deviation
-------------------
JAN 5.19 3.75
FEB 0.36 -1.08
MAR 4.28 2.84
APR 3.10 1.66
MAY 0.04 -1.40
JUN 0.00 -1.44
JUL 0.00 -1.44
AUG 0.00 -1.44
SEP 0.00 -1.44
OCT 0.32 -1.12
NOV 1.13 -0.31
DEC 2.82 1.38
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.