comparing array elements with a return value from a method

Viewed 107

I have a method that calculates the minimum and maximum amount of rainfall from a user input array of months. My problem is that my method returns the quantity of min and max and I need it to return the month that had the highest or lowest amount of rainfall. I am not sure how to compare my min and max methods to the months array. I imagine if I could say the min was found at index 5 then have a way to pull index 5 of the month array as well.

I have tried multiple types of for loops and nested for loops but can not seem to get the correct month.

public static void main(String[] args) {

    String[] months = {"January", "February", "March","April","May","June","July","August","September","October","November","December"};
    double[] rainfall = new double[months.length];

    Rainfall amazon = new Rainfall();

    //scanner to get user input
    Scanner scanner = new Scanner(System.in);

    //for loop to iterate through the months to get user input of rainfall each month
    for(int i = 0; i<rainfall.length; i++) {
        System.out.println("How much rainfall did you recieve, in inches, for the month of: " + months[i]);
        rainfall[i] = scanner.nextDouble();

        //while statement to reject negative numbers         
        while(rainfall[i] < 0) {
            System.out.println("You can not enter a negative number");
            System.out.println("How much rainfall did you recieve for the month of: " + months[i]);
            rainfall[i] = scanner.nextDouble();  
        }
    }

    System.out.println("total rainfall: " + amazon.getTotalRainfall(rainfall) + 
                       " average Rainfall: " + amazon.getAverageRainfall(rainfall) + 
                       " min: " + amazon.getMinRainfall(rainfall) + 
                       " max: " + amazon.getMaxRainfall(rainfall));

}

public double getMaxRainfall(double[] rainfall) {
    double max = rainfall[0];
    for(int i = 1; i < rainfall.length; i++)
        if(rainfall[i] > max) {
            max = rainfall[i];
        }
    return max;
}      

public double getMinRainfall(double[] rainfall) {
    double min = rainfall[0];
    for(int i = 1; i < rainfall.length; i++)
        if(rainfall[i] < min) {
            min = rainfall[i];
        }
    return min;
}

I need to know the month that had the highest and lowest amount of rainfall

4 Answers

The way you could solve this is by keeping track of the index of the highest rainfall instead of the value. The problem you have now is that you have 2 different arrays and the value from one does not relate back to the other.

For this you can do something like this:

    public int getMaxRainfallIndex(double[] rainfall) {
        int max = 0;
        for (int i = 1; i < rainfall.length; i++)
            if (rainfall[i] > rainfall[max]) {
                max = i;
            }
        return max;
    } 

You can then use this return value to get the actual rainfall and month from the original 2 arrays.

A cleaner approach however, would be to make a separate class to store both pieces of information so you can return the whole object containing both pieces of information together. This way you wouldn't even have to worry about keeping 2 separate arrays.

That would be done like this:

    public static void main(String[] args) {
        Rainfall[] rainfall = new Rainfall[12];
        //Do everything else
    }

    public class Rainfall {
        private String month;
        private double rainfall;

        //Constructor, Getters + Setters
    }

A possible solution will be to return the index of the month, so for example your getMaxRainfall method should look like this:

 public int getMaxRainfallIndex(double[] rainfall)
      {
        int maxIndex = 0;
        double max = rainfall[0];
              for(int i = 1; i < rainfall.length; i++)
                 if(rainfall[i] > max)
                    {
                     max = rainfall[i];
                     maxIndex = i;
                    }
              return maxIndex;
     }   

And in your main you should use that index to get both the month and the amount of rainfall:

int index = amazon.getMaxRainfallIndex(rainfall);
system.out.println("month of max rainfall: " + months[index]);
system.out.println("max rainfall: " + rainfall[index]);

Instead of managing two arrays separately, you could create a separate class:

public class RainfallByMonth {
    private double rainfall;
    private String month;

    // Getter/Setters here
}

In your main method:

public static void main(String[] args) {
    RainfallByMonth[] rainfallByMonths = new RainfallByMonth[12];
    String[] months = new DateFormatSymbols().getMonths(); // array of months

    Scanner scanner = new Scanner(System.in);
    for (int i = 0; i < rainfallByMonths.length; i++) {
        System.out.println("How much rainfall did you receive, in inches, for the month of: " + months[i]);
        rainfallByMonths[i].setMonth(months[i]);
        rainfallByMonths[i].setRainfall(scanner.nextDouble());
    }
    RainfallByMonth maxRainfall = getMaxRainfall(rainfallByMonths);
    // Now you can print the results here
}

Calculating max rainfall by month:

public static RainfallByMonth getMaxRainfall(RainfallByMonth[] rainfallByMonths) {
    RainfallByMonth maxRainfall = rainfallByMonths[0];
    for(int i = 1; i < rainfallByMonths.length; i++) {
        if(rainfallByMonths[i].getRainfall() > maxRainfall.getRainfall()) {
            maxRainfall = rainfallByMonths[i];
        }
    }
    return maxRainfall;
}

Similarly, the vice versa for the min case (only the condition will be different). This will be much easier to manage.

public static void main(String[] args) {
      String[] months = {"January", "February", "March","April","May","June","July","August","September","October","November","December"};
      int[] weather = {10, -1, 7,44,33,6,22,54,89,21,8,51};
      int max = 0;
      int index= 0;
      for (int i=0;i<weather.length-1;i++  ){
          if (weather[i] > m){
              max = weather[i];
              index= i;
          } 
      }
      System.out.println(months[index]); 
}

An example program to find highest month. To find lowest month rainfall go with less than symbol in if condition

Related