Java Rainfall Array Program

Overview in short for Java Rainfall Array Program –

Get the total yearly rainfall. Also, Iterate through the Array and print the average monthly rainfall, Print the min and most rainfall value with the month name.

Java Rainfall Program Assignment Details-

A file exists which contains the total rainfall for each month in a year, one double value on each line. Write a program which:

  1. Asks the user to type in the name of the file.
  2. Reads the data from that file storing each value in an Array.
  3. Iterates through the Array and prints out the total rainfall for the year.
  4. Iterates through the Array and prints out the average monthly rainfall.
  5. Iterates through the Array and prints out the month with the minimum rainfall and how much that was. (Printing the name of the month will earn extra marks).
  6. Iterates through the Array and prints out the month with the most rainfall and how much that was. (Again converting the month number to a String will earn extra marks).

Rain Fall Data in Text File-

3.75
1.80
11.85
2.70
7.30
4.40
12.100
6.15
9.60
8.45
10.90
5.20

Java source code solution for rainfall array program. Included extra bonus point


import java.util.Scanner;
import java.io.*;
import java.text.DateFormatSymbols;

public class RainFall {

    static String inttoMonth(int month){
        String monthName=new DateFormatSymbols()
                             .getMonths()[month - 1];
        return monthName;
    } 

    public static void main(String[] args) 
        throws IOException, FileNotFoundException {

    System.out.println("Enter the file name to read rainfall data");
    Scanner sc = new Scanner(System.in);
    String fileName = sc.next();
    // System.out.println(fileName);
    sc.close();

    double[] rainData = new double[12];
    double[] tempData = new double[12];

    File file = new File(fileName);
    BufferedReader br = new BufferedReader(
                        new FileReader(file));

    String st;
    double d;
    int i = 0;
    while ((st = br.readLine()) != null) {

        d = Double.parseDouble(st);
        // System.out.println(d);
        rainData[i] = d;
        tempData[i] = d;
        i++;
    }

    br.close();

    double totalRainfall = 0;
    for (i = 0; i < rainData.length; i++) {
        // System.out.println(rainData[i]);
        totalRainfall += rainData[i];
    }
    System.out.printf("\nTotal rainfall (year) : %.2f",
                      totalRainfall);
    System.out.printf("\nAverage Monthly rainfall: %.2f",
                      totalRainfall / 12);
    System.out.println("\n");
    // System.out.println("File read successfully");

    double temp;
    double minRainFall = 0;
    double maxRainFall = 0;

    for (i = 0; i < tempData.length; i++) {
        temp = 0;
        for (int j = 1 + i; j < tempData.length; j++) { if (tempData[i] > tempData[j]) {
                temp = tempData[i];
                tempData[i] = tempData[j];
                tempData[j] = temp;

            }

        }
        minRainFall = tempData[0];
        maxRainFall = tempData[tempData.length - 1];
    }

    int minRainMonth = 0;
    int maxRainMonth = 0;
    for (i = 0; i < rainData.length; i++) {
        if (rainData[i] == minRainFall) {
            minRainMonth = i + 1;
        }
        if (rainData[i] == maxRainFall) {
            maxRainMonth = i + 1;
        }
        // System.out.println(tempData[i]);
    }

    System.out.println(minRainFall + 
        " is the min Rainfall in month of " 
        + inttoMonth(minRainMonth));
    System.out.println(maxRainFall + 
        " is the max Rainfall and in month of " 
        + inttoMonth(maxRainMonth));

    }

}

Output on Java terminal

Enter the file name to read rainfall data
data.txt

Total rainfall (year) : 84.20
Average Monthly rainfall: 7.02

1.8 is the min Rainfall in month of February  
12.1 is the max Rainfall in month of July

Finally, if you need step by step code explanation and to get more such Java Homework Help, leave us a message.

Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed.