0

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)
    {
    }
}
}
3
  • Where does the error occur exactly? Commented Feb 11, 2014 at 2:34
  • k--; <<<<< you are in troubles when k == 0 Commented Feb 11, 2014 at 2:37
  • @willl, the for loop adds one when the loop is finished, so when k == 0, k-- will equal -1, and the for loop adds one, returning the value to 0. Commented Feb 11, 2014 at 2:42

3 Answers 3

2

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];
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.