0

Okay so I am getting an error for my code that is trying to count how many of each integer there are in the array. And so im not sure how to fix it. Im getting the error on the while loop at the bottom. Any help would be appreciated. Thanks.

            if (ndx != array.length)
                while (array[ndx] == array[ndx + 1])
                    ndx++;
3
  • Do you know what this code is trying to do? what happens if I pass in null? or an empty array with no elements? There is also no reason to have a nested for loop to iterate over an array once and print out its values. You should probably break this apart and rename some variables...or you may want to start over. Commented Nov 27, 2012 at 0:25
  • yea it is quite confusing but this is only part of the whole code but i already got it to work with an empty array and null you get a response to input a new file. But basically i think the problem is that the while loop is doing "ndx + 1" but when it gets to the end of the array its getting the error because you cant index past the array. Im just not sure how to fix it. Commented Nov 27, 2012 at 0:31
  • Sure. That means that you need to check that you're not about to go out of bounds of the array, so you need to check the value of ndx and make sure it's < array.length (not != ) before you increment. Nevertheless, this is very ugly code that would likely cause an instructor to deduct many points. Consider renaming your variables to clear names (like index instead of ndx) and using functions to show what you're trying to do. (like findMatches()) instead of that inner for loop. Commented Nov 27, 2012 at 1:05

2 Answers 2

1

On the last iteration of the outer for loop, ndx is one less than array.length, so when you call array[ndx+1], that is equivalent to array[array.length], which out of bounds, since arrays start indexing at 0. Changing your bottom if statement to:

if (ndx != array.length-1)

should do the trick.

It's also incrementing ndx in the final while loop, so there should be a conditional to check for that, too:

while(ndx != array.length-1 && array[ndx]==array[ndx+1]) 

Since you take care of the array.length-1 condition in the while now, you can get rid of the if that had it in the above line. Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

0

You get the exception because of this line of code:

while (array[ndx] == array[ndx + 1])

On the last iteration of the loop, ndx is equal to array.length - 1

When you try to access array[ndx + 1] you are attempting to access an array index that is out of range.

You should change the if statement above the while loop to this:

if (ndx != array.length - 1)

3 Comments

Tried this but it didn't work it keeps giving me the same error.
@ShamWowSnuggie - Actually, your problem is that you increment the value of ndx inside your while loop, which could eventually cause its value to be too large (and out of range). Your while loop should also make sure the value of ndx is valid.
Check out the answer by @awolfe91... His edit shows how to correct this problem.

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.