Skip to content
Advertisement

ArrayIndexOutOfBoundsException when finding words in an array that and with a specific letter

I am trying to find words in an array that end with a letter ‘a’. I thought of doing it using two for loops but I keep getting integer out of bounds error.

Could anyone tell me what i am doing wrong?

The code:

JavaScript

Advertisement

Answer

You’ve got too much code for the task, which has lead to a bug creeping in and hiding in plain sight. As a general rule, keeping code as simple as possible results in less bugs.

Delete this, which you don’t need.

JavaScript

And delete all of this:

JavaScript

instead basing your code on endsWith("a"):

JavaScript

which is easy to read and understand (and thus easier to avoid bugs).


Even simpler, since you don’t need to reference the array, use the result of the split directly:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement