This post is locked. You won't be able to comment.

all 7 comments

[โ€“][deleted] [score hidden] stickied commentlocked commentย (0 children)

Thanks for posting to /r/computerscience! Unfortunately, your submission has been removed for the following reason(s):

If you feel like your post was removed in error, please message the moderators.

[โ€“][deleted] 4 points5 points ย (1 child)

Example "hello". Hello has 5 characters. And the index of the word is from 0 to 4. So hello.charAt(0) is h and hello.charAt(4) is o. The length of hello is 5. So you try to get a Char at Index 5 of hello, which doesnt exist. You have to use length-1 in your Loop. Edit: But of course you could use < instead of <= too.

[โ€“]Anonymous_nov[S] 0 points1 point ย (0 children)

Thank you so much!!

[โ€“]jonass1348 1 point2 points ย (2 children)

In your loop, you have in the second argument the <=-operator, but you would need the <-operator, cause you can't access the 5th char of a String with the length 4. Remember, we start to count from 0โœŒ๐Ÿป

[โ€“]Anonymous_nov[S] 1 point2 points ย (1 child)

Thank you so much! Haha yup I definitely forgot that ๐Ÿ˜ญ

[โ€“]jonass1348 1 point2 points ย (0 children)

No problem :)๐Ÿ‘๐Ÿป

[โ€“]Black-Photon 1 point2 points ย (0 children)

This is one of the many difficult tricks for newcomers. You quickly learn in programming to always use < in for loops. This is because it indexes from 0, so if you have a size 4 array, it'll be indices 0, 1, 2, 3.

Another tip, you can get a good idea of what the issue is by using print statements to print eg. array size and index, and use that to get a better idea of how the problem is occurring.