Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringIndexOutOfBounds Exception

 
Charmaine Gatling
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a program that searches multiples strings for a certain set of characters (substring) and returns the index of that substring, I get this StringOutOfBoundException. I'm using String.index() and it works for about 25 iterations then I get this error. What am I not doing?
 
Tom Purl
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help tremendously if you would post a portion of your code. Thanks!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll guess that the exception that you were actually getting was the StringIndexOutOfBoundsException. Taking a look at the documentation for StringIndexOutOfBoundsException we find a simple explanation for when this exception is thrown:
Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.
Are you sure that you aren't trying to search a String beyond it's length? Are you getting any ideas?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic