-5

I have been working on some code in java and I just ran into an error and i'm not sure why because I just ran some code that was just like this and it worked just fine.

    String [] month = {"Jan", "Feb", "Mar", "Apr", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

    double [] temperature ={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};    
    double [] precipitation ={3.5, 3.4, 4.3, 2.9, 3.2, 6.8, 6.1, 6.6, 4.4, 2.5, 2.2, 2.6};  
 for( int index = 0; index < temperature.length; index++)
    {
        System.out.print(""+month [index] +".        "+temperature [index]+"         "+precipitation [index]);
        System.out.println();
    }

1 Answers1

1

From inspection, I can see that your month array is missing the month of May, and therefore only has 11 elements in it. As a result, when you iterate over the 12 elements in the temperature array, you will get an array out of bounds exception during the final temperature, because there is no corresponding enter in month at that position.

To fix this, just make sure that month and temperature are the same size:

String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
    "Oct", "Nov", "Dec" };
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360