简体   繁体   中英

ArrayIndexOutOfBounds Exception with a 2-dimensional array

Here we go... when I try an example with 3 students and 3 classes, the input works fine until the last class, where it throws an exception. This doesn't make sense because the length should fit the for loop... Can anyone find what's wrong here?

import java.io.*;

public class Application
{
public static void main()
{
    int studentNum = 0;
    int courseNum = 0;

    System.out.println("\f------GRADE CALCULATOR------\n");
    try
    {
        BufferedReader buffin = new BufferedReader(new InputStreamReader (System.in));
        System.out.print("Enter number of students: ");
        System.out.flush();
        studentNum = Integer.parseInt(buffin.readLine());
        System.out.print("Enter number of courses to compute grades for: ");
        System.out.flush();
        courseNum = Integer.parseInt(buffin.readLine());

        int grades[][] = {new int[studentNum], new int[courseNum]};
        System.out.println("\nEntering grades for " + studentNum + " students for " + courseNum + " classes.\n");
        System.out.println(grades.length);
        for (int i = 0; i < studentNum; i++)
        {
            System.out.println("Entering grades for student #" + (i+1) + "...");
            for (int k = 0; k < courseNum; k++)
            {
                System.out.print("Enter grade recieved in course #" + (k+1) + ": ");
                System.out.flush();
                char letterGrade = (buffin.readLine()).charAt(0);
                if (letterGrade == 'A' || letterGrade == 'a')
                    grades[i][k] = 4;
                else if (letterGrade == 'B' || letterGrade == 'b')
                    grades[i][k] = 3;
                else if (letterGrade == 'C' || letterGrade == 'c')
                    grades[i][k] = 2;
                else if (letterGrade == 'D' || letterGrade == 'd')
                    grades[i][k] = 1;
                else if (letterGrade == 'F' || letterGrade == 'f')
                    grades[i][k] = 0;
                else
                {
                    System.out.println("\nInvalid entry!  Acceptable inputs are A, B, C, D, or F.\n");
                    k--;
                }
            }
        }
    }
    catch (IOException e)
    {
    }
}
}

This line of code is the issue:

int grades[][] = {new int[studentNum], new int[courseNum]};

You are initializing an array that has two elements: an array of studentNum elements, and an array of courseNum elements.

What you should be doing is this:

int grades[][] = new int[studentNum][courseNum];

The problem is that you're creating grades as a length two array, the elements of which are arrays. You probably intended to create a multidimensional array like this.

int[][] grades = new int[studentNum][courseNum];

This will create an array of studentNum elements, each of which is initialized to an array of courseNum ints.

int grades[][] = {new int[studentNum], new int[courseNum]};

What you are creating here is an array with the length of 2.

  • At index 0 you have new int[studentNum] and
  • at index 1 you have new int[courseNum]

So you have an array of two arrays with different lengths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM