1

I created an array within a for loop to generate the cube of 1-9 in descending order. My code appears to work as I am able to run it without any syntax or runtime errors. However, whenever I try to use the length() method in my for loop, I get an "array out of bounds exception".

Here is my code without the length() method:

/**
 * This method prints out a cubes from one to nine in descending order
 */
public static void cubes()
{
    // create a fixed length array and hard code index number
    int[] values = new int[9];
    values[0] = 1;
    values[1] = 2;
    values[2] = 3;
    values[3] = 4;
    values[4] = 5;
    values[5] = 6;
    values[6] = 7;
    values[7] = 8;
    values[8] = 9;
    // Create variable to store cubed numbers
    double cubedNumber = 0;
    // Create for loop to run the array from 1-9 in descending order
    for (int counter = 8; counter > 0; counter--)
    {
        cubedNumber = Math.pow(values[counter], 3);
        System.out.println(values[counter] + " cubed is " + cubedNumber);
    }
}

This is my code using the length() method:

/**
 * This method prints out a cubes from one to nine in descending order
 */
public static void cubes()
{
    // create a fixed length array and hard code index number
    int[] values = new int[9];
    values[0] = 1;
    values[1] = 2;
    values[2] = 3;
    values[3] = 4;
    values[4] = 5;
    values[5] = 6;
    values[6] = 7;
    values[7] = 8;
    values[8] = 9;
    // Create variable to store cubed numbers
    double cubedNumber = 0;
    // Create for loop to run the array from 1-9 in descending order
    for (int counter = 8; counter <= values.length; counter--)
    {
        cubedNumber = Math.pow(values[counter], 3);
        System.out.println(values[counter] + " cubed is " + cubedNumber);
    }
}

Which gives me the following error: "java.lang.ArrayIndexOutOfBoundsException: -1 at arraysPractice.cubes(arraysPractice.java:31)" I'm required to use the length method in my for loop. Am I using the length() method incorrectly? In both instances, the program still generates the cubes per this output

5
  • values.length is 9. This loop doesn't make sense. Why do you want to replace > 0 with <= values.length? These are obviously not equivalent. You probably want to replace counter = 8 with counter = values.length - 1. Commented Nov 18, 2017 at 18:23
  • I'm required to use values.length() method somewhere in the for loop, so I thought I had to use it there. I just tried the values.length - 1 and while it appears to work, I'm rather stumped as to why. What does it mean when to use the length() method minus a 1? Commented Nov 18, 2017 at 18:29
  • Well, values.length is 9. Not 0. It's the length of the array. You want to start your loop at the last index, which is equal to length - 1. Read my comment again. Commented Nov 18, 2017 at 18:30
  • Oh I think I'm beginning to understand. The loop is going in descending order so I need to start at -1 value, which is 9? Commented Nov 18, 2017 at 18:33
  • No, you don't want to start at -1. You want to start at 8, just like in your working loop. You start at 8 because it's the last index of the array, which has a length of 9. So, instead of hard-coding 8, you rather use values.length -1, so that, if you then decide to use an array of 20 elements, the loop continues to start at the last index, which would then be 19. Don't you realize that length - 1 and -1 are not the same thing? Commented Nov 18, 2017 at 18:36

2 Answers 2

1

The issue is here:

for (int counter = 8; counter <= values.length; counter--)

You're decrementing the counter; it only goes lower over time. It will always be <= values.length.

Eventually it will reach -1, and produce an ArrayIndexOutOfBoundsException.

Maybe you want...

for (int counter = values.length - 1; counter >= 0; counter--)
Sign up to request clarification or add additional context in comments.

Comments

0

Your condition is counter <= values.length

values.length have the value 9 and counter value starting decreasing from 8 to 7,6,5,4,3,2,1,0 and when its value reached at -1, still condition counter <= values.length is true for -1. And in this code

cubedNumber = Math.pow(values[counter], 3);

When you pass -1 as index in values array, then it will give you the java.lang.ArrayIndexOutOfBoundsException Exception.

You loop in this way to made it run fine.

for (int counter = values.length - 1; counter >= 0; counter--)
{
    cubedNumber = Math.pow(values[counter], 3);
    System.out.println(values[counter] + " cubed is " + cubedNumber);
}

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.