Reputation: 17
I am using a 3D array int the below code i am getting array index out of bound error below is the code for the same:-
package array2d;
import java.util.Scanner;
public class Array3 {
public static void main(String[] Args) {
Scanner vee= new Scanner(System.in);
int penilaian[][][];
int mahasiswa[];
for (int i = 0; i < 5; i++) {
System.out.print("Class" + (i + 1) + " : ");
int mhs = vee.nextInt();
System.out.println("------------------------------------------------");
mahasiswa = new int[mhs];
penilaian = new int[i][mahasiswa.length][3];
for (int j = 0; j < mahasiswa.length; j++) {
System.out.print("Score Mid Test" + (i + 1) + " Ke -" + (j + 1) + " :");
penilaian[i][j][0] = vee.nextInt();
System.out.print("Score Test" + (i + 1) + " Ke -" + (j + 1) + " :");
penilaian[i][j][1] = vee.nextInt();
System.out.print("Score Project" + (i + 1) + " Ke -" + (j + 1) + " :");
penilaian[i][j][2] = vee.nextInt();
System.out.println("=======================================================================");
}
}
}
}
Getting the below error:-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at array2d.TugasArray3.main(TugasArray3.java:27) Score Mid Test1 Ke -1
Upvotes: 0
Views: 2472
Reputation: 16
you declare a three-dimensional (3d) array penilaian = new int[i][mahasiswa.length][3];
when i=0
the size of first element is 0
therfore you cant assign any value to this array
Upvotes: 0