The program asked the user how many inches of rainfall per month and the user inputs the amount 12 times since their are 12 months in a year and after they input the number of inches of rainfall for 12 months, its suppose to sum up all of the inputs:
import java.util.Scanner;
public class RainFall {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int MONTHS=12;
double rainfall[]=new double[MONTHS];
Scanner keyboard=new Scanner(System.in);
System.out.println("The total rainfal is "+totalRainFall(rainfall));
}
public static double totalRainFall(double rainfall[]){
int total=0;
int MONTHS=12;
for(int index=0; index<MONTHS;index++){
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter the total rainfall");
rainfall[index]=keyboard.nextDouble();
total+=rainfall[MONTHS];
}
return total;
}
}
Enter the total rainfall 89.9 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12 at RainFall.totalRainFall(RainFall.java:27) at RainFall.main(RainFall.java:11)
But now I get an out of bound error when I compile and executed the code even though their are no compilation errors as shown right above this comment.
I have executed this code before with no issue, but now I am getting errors. I started getting errors when I replace my for loop with a while loop , and then put my original forloop back.