Im trying to create a code that records and stores the identity and the amount of milk produced by everytime a cow is milked, 2 times a day, and for 1 week( 7 days) It also needs to show the cow that has milked the most for 1 week, and all the cows that have produced milk below 12 litres. here is simplified code where the error is genearted:
import java.util.Scanner;
public class test
{
public static void main(String[] args)
{
Scanner na = new Scanner(System.in);
System.out.println("Please enter number of cows: ");
int num = na.nextInt();
int[] ID =new int[num];
double[] Milk = new double[num];
double[] Milk2 = new double[num];
double[] total = new double[num];
double[] Average = new double[num];
double[] Lcows = new double[num];
double[] Lmilks = new double[num];
Scanner input = new Scanner(System.in);
for (int i = 0; i < num; i++)
{
System.out.println("Enter cow ID no." + (i+1));
ID[i] = input.nextInt();
if (ID[i] < 1000 && ID[i] > 0)
{
for (int u = 0; u < 7; u++)
{
System.out.println("DAY " + (u+1));
System.out.println("enter milk produced for first session");
Milk[u] = input.nextDouble();
total[i] = total[i] + Milk[u];// error can be found here.
System.out.println("enter milk produced for second session");
Milk2[u] = input.nextDouble();
total[i] = total[i] + Milk2[u];// here as well.
}
}
}
}
}
I was testing out the code but got this error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at test.main(test.java:37)
I have no idea what i am doing wrong! I need your smart help :):