Having issues with array.

Seems to pull in the enum files (month and average rainfall) and print accordingly.

Also seems to read in the updated rainfall per month.

However, problem is I can not make updated print with the enum.

Also, last line in program has ..."actual[i]". If use a value for the i, will get that value printed. If leave with variable, get error message which points to this issue....."at Rainfall.main(Rainfall.java:52)"

Any help appreciated.

import java.util.Scanner;

public class Rainfall
{

   public enum Average
   {
     // Rainfall averages are from the internet and for Cleveland, Ohio

     January (2.72), February (2.34), March (2.93), April (3.49), May (3.66), June (3.43),
     July (3.46), August (3.51), September (3.81), October (3.07), November (3.62), December (3.10);

     private double average, actual;

     private Average(double average)
     {
         this.average = average;        
     }

     public double getAverage()
     {
        return average;
     }

    }

    public static void main(String[] args)
    {
      Average[] CurrentMonth = Average.values();
      Scanner keyboard = new Scanner(System.in);
      double [] actual = new double[12];
      int i;

      for ( i=0; i<CurrentMonth.length; i++)
      {
          System.out.println("Enter rainfall average for "+ CurrentMonth[i]);
          actual[i] = keyboard.nextDouble();
      }



      System.out.println("Test for rainfall.");
       for(Average p1 : Average.values())
      {
        System.out.println("Month average: " + p1.name() +" : " + p1.average + "  " + actual[i]);
      }

   }
}

Recommended Answers

All 9 Replies

how exactly is that line, if you leave the value away?

After the for loop (Lines 34-38) i will be >= CurrentMonth.length, so that's why you have the error - array index out of bounds on line 45

commented: indeed +13

if I leave line 36 as is.... CurrentMonth[i])... and modify line 53 to ...."actual[i-1]"....output for December (last output line) is...."Month average: December : 3.1 3.0"..... The value of "3" is the last value from the user. All months show thislast "i".

If I leave line 36 alone and make the last line ..."actual[i]"... it compiles (no sintax errors), but the output runs through the 12 months then cuts out with ..." java.lang.ArrayIndexOutOfBoundsException: 12
at Rainfall.main(Rainfall.java:52)"...

I know this means my array ended, but why (where)?

Also, I am using two loops - should it be one ? I am trying to pull in the enum data and add user input for the current data. End result will be to find difference between actual and average and print. Should I have one loop, can I add the loops ?

Thanks.

arrays in Java are 0 based, meaning the indices for an array of length 12 (like yours) vary from 0 (first element) to 11 (last element)

in your for loop, you are augmenting the value of i, basically saying: 'run this until the value of i is higher than the last valid index'.

after that, you don't change the value of i, yet you do ask the element with the index i. but at this point, i is already out of range.

i fixed in line 31 by replacing the new double [12] to [13]. This eliminated the array error. Still issues with the two loops not talking with each other. How do i combine the enum data with the user input data ?

That doesn't fix the error - you have 12 data elements so the array should be [12]. What you did just "hid" the error.

Your problem comes down to this: you have some data in an array, but you are using an enhanced for loop on a different array (ie Average.values()), so it's hard to reference both afrrays in the loop.
The solution is to use a for (i=0... type of loop, in which you can access both actual[i] and Average.values()[i]

so you're suggesting;
1) Leave line 31 as 12
2) keep lines 24 - 38 (as they are the input lines)
3) change lines 42 to 46 (output lines) ?

How to do so (chane output) to grab both new input and the enum lines. This may be my issue. Books I've looked at don't show how to grab both, just output enum.

Thanks.

Like I said before, Average.values() is an array of enum values, it's a loop along this pattern (pseudo code):

for i = 0 ... 11 {
   print actual[i]
   print Average.values[i].getAverage()
}

Got it. Thanks !!!

Started new post with second part of this. Hope to see you there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.