0
import java.util.Scanner;

class array2 {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
            int arr_count = input.nextInt(), i;
                System.out.print("Jumlah Data : ");
                arr_count = input.nextInt();

    
            int score [][] = new int[arr_count][10];
            String subject [][] = new String[arr_count][1];
            float total, avrg;
            
            for (i=0;i<arr_count;i++) {
                System.out.print("Mata Kuliah : ");
                subject [i][0] = input.next();
                System.out.print("Nilai Teori : "); score [i][0] = input.nextInt();
                System.out.print("Nilai Praktik : "); score [i][1] = input.nextInt();
                System.out.println();
            }
            
 
            System.out.println();
            System.out.println("----------------------------------------------------");
            System.out.println("Mata Kuliah     Teori     Praktikum       Rata-rata");
            System.out.println("----------------------------------------------------");
            
            total = score [i][0] + score [i][1];
            avrg = total/2;
            
        
            System.out.print("\t"+subject+"\t"+score [i][0]+"\t"+score [i][1]+"\t"+avrg);
            
      }
}

error = java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 at array2.main

How do I fix this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0
total = score [i][0] + score [i][1];

This line expect to get "i" but loop already closed.

To fix - close for loop after printing this line:

System.out.print("\t"+subject+"\t"+score [i][0]+"\t"+score [i][1]+"\t"+avrg);
Sergey
  • 46
  • 4