-2

I'm trying to make a small app to practice 2D arrays. I basically want to collect grades and then compute the maximum and minimum grade.

However when I run the code I get an ArrayIndexOutOfBoundsException: error, which points to this segment of code:

for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 10; j++) {
            results[i][j] = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the " + (i + 1) + " result for the " + (j + 1) + " student."));
        }
    }

This is where I collect the user input.

Here is where I setup the array.

double max, min;
double results[][];
results = new double[3][10];

So, as I am not getting any errors, I'm not sure what the cause is. It seems I'm trying to access something that isn't available, but how come it can't be accessed?

Calum
  • 1,889
  • 2
  • 18
  • 36
joe martin
  • 93
  • 1
  • 11
  • 1
    What makes you think that the array indices are in range from 1 to 10 (or 3)? – Tom Sep 26 '15 at 21:29

1 Answers1

0

Java arrays are 0-index based.

Use:

for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 10; j++) {
    ...
  }
 }

instead.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Even after I make these changes, I still get the same error. It's on this line specifically results[i][j] = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the " + (i + 1) + " result for the " + (j + 1) + " student.")); – joe martin Sep 26 '15 at 21:33
  • Try this: replace that line with `results[i][j] = 0;`. The exception you describe is actually to do with the assignment, not how you are entering numbers. If you *still* get an exception, you've not got the correct loop guards still. – Andy Turner Sep 26 '15 at 21:36