4

Been looking around to see if anything could help me out but I don't understand much of what people are answering and anything I do understand doesn't seem to solve the issue! So basically as the title says, I'm getting an array index out of bounds exception and I have no idea why. Any help is greatly appreciated.

Code:

import javax.swing.*;

public class Array {
    public static void main(String[] args) {
        double height[] = new double[10];
        String heightAsString;
        int i, over18 = 0, under16 = 0;

        for(i = 1; i <= height.length; i++){
            heightAsString = JOptionPane.showInputDialog("Please Enter The Height Of Person " + i);
            height[i] = Double.parseDouble(heightAsString);

            if(height[i] > 1.8){
                over18 += 1;
            }

            if(height[i] < 1.6){
                under16 += 1;
            }
        }

        JOptionPane.showMessageDialog(null,"The Total Number Of People Over 1.8m Is: " + over18 +
        "\nThe Total Number Of People Under 1.6m Is: " + under16);
    }
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
SavageSpud
  • 125
  • 1
  • 4
  • 11
  • and why do you start with height[1] ? – 12dollar Apr 21 '15 at 14:46
  • @MarounMaroun It asks for 9 people then instead of 10. When I have set the array size to 10.... – SavageSpud Apr 21 '15 at 14:47
  • @SavageSpud I made my comment an answer to explain more. – Maroun Apr 21 '15 at 14:47
  • @SavageSpud Arrays in Java are zero-indexed, meaning if you create an array of size `10`, it will have indices from `0` to `9`. Thus, `for(i = 1; i <= height.length; i++){` causes the exception. `i` should start at `0` and run as long as`i < height.length`. – Turing85 Apr 21 '15 at 14:50
  • You should use the debug mode in order to see the state of your objects at run time. It'll help you to understand how this loop works. – Loci Apr 21 '15 at 14:54

7 Answers7

7

Change to

i = 0; i < height.length
    ↑    ↑

Arrays are zero-based in Java. Meaning that if you have an array of size N, the indexes will be in range [0, N - 1], see The Java™ Tutorials - Arrays:

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

If you're interested (recommended), go through the JLS - Chapter 10. Arrays as well:

If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.

Maroun
  • 94,125
  • 30
  • 188
  • 241
5
for(i = 1; i <= height.length; i++){
        heightAsString = JOptionPane.showInputDialog("Please Enter The Height Of Person " + i);
        height[i-1] = Double.parseDouble(heightAsString);

        if(height[i-1] > 1.8){
            over18 += 1;
        }

        if(height[i-1] < 1.6){
            under16 += 1;
        }
    }

use height[i-1], because array index starts from 0.

The Guest
  • 698
  • 11
  • 27
3

The problem is with = in i <= height.length condition of the loop

 for(i = 1; i <= height.length; i++){

Change to

 for(i = 0; i < height.length; i++){

height.length will give you number of element in array and array index start from 0 to and ends height.length-1 so in the last iteration of loop you are trying to access height.length index of array which doesnt exist giving you ArrayIndexOutOfBounds Exception.

singhakash
  • 7,891
  • 6
  • 31
  • 65
0

The array is always indexed from zero, therefore it is recommended and clean, to not go around it and use it as it is.

So there are two problems with your code - first is, that you start from one, which does not throw exception, but it is not good approach.

Second is, that you reached limit of array because of indexing from zero (array with size 10 is accessed with indexes from 0 to 9).

Your code should look like this :

    for(i = 0; i < height.length; i++){
        heightAsString = JOptionPane.showInputDialog("Please Enter The Height Of Person " + (i+1));
        height[i] = Double.parseDouble(heightAsString);

        if(height[i] > 1.8){
            over18 += 1;
        }

        if(height[i] < 1.6){
            under16 += 1;
        }
    }
libik
  • 22,239
  • 9
  • 44
  • 87
0

The problem isn't in the for loop declaration, it's in this line:

height[i] = Double.parseDouble(heightAsString);

You're starting your for loop i value at 1, you have declared your array as having 10 items, indexed from 0 to 9.

as a result, when you try and assign the index value 10, you receive an index out of bounds error.

This will solve:

height[i-1] = Double.parseDouble(heightAsString);
Loco234
  • 521
  • 4
  • 20
0

In java array index starts from zero. So if you declare an array like

double height[] = new double[10];

its index starts from zero and ends at 9.

But in your code, your index variable(i) starts from 1 and ends at 10(height.length). It leads to two issues, So you are missing first element (0th) in array, and trying to get 10th element (which is not present), and it throws Array Index Out Of Bounds Exception. Change your for loop so that it starts from zero, and ends at 9.

for(i = 0; i < height.length; i++)

Jomy George
  • 313
  • 3
  • 13
0

Change the following line of code

for(i = 1; i <= height.length; i++){

by the following to solve two problems you have:

for(i = 0; i < height.length; i++){

The problem in the first line of code is you are missing the first position (i = 0) of the array. Since every array starts at the 0 position so your last index cannot be equal the length of the array, but 1 index less, and this is why you are having the index out of bound error.

To better understand consider you have the following 3 position array: [0] [1] [2].

Its length is 3, because there is 3 memory space allocated. But, to access the last space of memory you have to use the index 2, and not 3 as the lenght. Due to that the i < height.length should be used instead of i <= height.length, so you 'i' index will never be the length and never get the index out of bound error.

MateusMnc
  • 1
  • 1