File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Searching Code: Out of Bound Index Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching Code: Out of Bound Index Error" Watch "Searching Code: Out of Bound Index Error" New topic

Searching Code: Out of Bound Index Error

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Hello everyone

I was working on a search method and I've almost completed it. However there's just one slight problem with it that can't seem to be able to fix. If I search for 1 character it works fine, but when I have two or more character the complier gives me an out of bound index error.

Here's the code:

And the error message:

java.lang.StringIndexOutOfBoundsException: String index out of range: 29
at java.lang.String.charAt(
at MainClass.main(
Exception in thread "main"

java:25 (line 25) is where there's c[j] = str1.charAt(i+j). I messed around a bit if the for loops that have the variable i and j but it seems to have no effect.

Any suggestions?
[ March 01, 2008: Message edited by: Olivier Legat ]

Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

what is it that you are actually trying to do in the inner forloop?

// j: Deviation to the right from i
for(int j=0; j<num; j++)
c[j] = str1.charAt(i+j);

Size of the string str1 is smaller than the index of (i+j). That is the reason for the arrayindexoutofboundsException.

try to access within the size bounds of the string str1.

I agree. Here's the link:
subject: Searching Code: Out of Bound Index Error
It's not a secret anymore!