0

I am getting an array index out of bounds exception while iterating over an array through the for-loop. Can someone tell me why this is happening it I have set the boolean in the for-loop to be i

public static boolean verify(int [] seq ){
  for (int i=0; i<seq.length; i++){

  //If the number is even, the next number
  //must the half the previous number
  if (seq[i] %2==0){
    if (seq[i+1] != (seq[i]/2)){
      return false;
    }
  }

  //If the number is positive, the next number
  //must be 3 times + 1 the previous number
  else if (seq[i] %2!=0){
    if (seq[i+1] != ((seq[i])*3+1)){
      return false;
    }
  }

}

}

5
  • Why don't you print out the value of i at every step? Or better yet, use a debugger. Commented Feb 3, 2014 at 17:56
  • can't call seq[i+1] when i=seq.length. Change loop condition to i<seq.length-1 Commented Feb 3, 2014 at 17:57
  • You can't use seq[i+1] when i+1 >= seq.length Commented Feb 3, 2014 at 17:58
  • "I have set the boolean in the for-loop to be i" you have WHAT? Commented Feb 3, 2014 at 18:00
  • This is a good opportunity to learn how to debug. If you don't have an effective debugger, use println statements. Commented Feb 3, 2014 at 18:08

6 Answers 6

1

The problem is when you access index i+1. If i is the last possible value (seq.length - 1), then i+1 is one beyond the end of the array, resulting in an ArrayIndexOutOfBoundsException.

Stop your for loop one iteration earlier by modifying your condition to be:

i < seq.length - 1
Sign up to request clarification or add additional context in comments.

Comments

1

You will face exception for the maximum value of i bcoz you are increasing the value by 1 to find the index value.

    if (seq[i] %2==0){
            if (seq[i+1] != (seq[i]/2)){
---------------------^
              return false;
            }
          }

Comments

1

You're trying to access position i+1 or the Array. Since your for loop goes until the last element, you'll try to access 1 position after the last element, what causes the Out Of Bounds exception.

Comments

1

You are iterating over all elements in the array, but checking element seq[i + 1] for i == seq.lenth - 1 will always cause the exception. The last number is fully constrained by your conditions, so no need to check it. Make your loop run as follows: for (int i=0; i <seq.length - 1; i++)

Comments

1

This:

    if (seq[i+1] != (seq[i]/2)) {

cannot access an element beyond the end of the array, when i is seq.length - 1.

Another line like that is down in the else branch.

Comments

1

Its quite obvious , when you are passing an array (i.e. array contains 10 elements) and operating inside loop that correct.But when you are accessing seq[i+1] , there might be the you are accessing the index which is not available in the array.

When the i value reaches at 10 and you are trying to access i+1 , but this index is not in array (as we know array size is 10)

So , its caused this exception. Hope it will help you.

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.