File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String index out of range Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String index out of range" Watch "String index out of range" New topic
Author

String index out of range

Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20
im having problems to figure it out where is probl
everytime i run this, string index out of range comes out as an error.

any suggestion please.

Thanks a lot.

Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

This line is problematic:

You're trying to access more char elements than you'r string has (as array's numbering starts at zero not one).
Try to change this line to this:
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

I added code tags to your post, making it easier to read. Next time, just highlight your java and click the "code" button - just like you were making it italic or bold.

Fixing this problem is simple enough. If a String has a length of 10, the characters are at positions 0-9. Your for loop should be "<", not "<=".

correct that, and you will find your next problem that needs fixing...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Grayson Churchel
Greenhorn

Joined: Aug 20, 2011
Posts: 7

Try changing your For statement to:
for (int ltr=0; ltr < sentence.length(); ltr++)
Since the string has a zero-based index (positions are 0 through n-1), your For statement is trying to index one place past the end of the string.
Example:
"Hello" : 0=H, 1=e, 2=l, 3=l, 4=o and 5=(error)
but the length of the string is 5 so you have to stop indexing through it at 4.
Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20
Thanks a lot guys, i don't have that error message anymore, and i understand now
but when i enter " Its April" , it just take the lowercase vowel

do i have to write a statement using equalsIgnoreCase() ??
Grayson Churchel
Greenhorn

Joined: Aug 20, 2011
Posts: 7

Then you need to either add more test cases for capitol vowels, OR lower-case the character you're testing so that the upper-case char's will be caught. I believe there's a function which will ensure the characters are lower case before you do the compare.
Remember, Java is case-sensitive.
Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20
Thanks a lot
i just did it and it works

Grayson Churchel
Greenhorn

Joined: Aug 20, 2011
Posts: 7

Happy to help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Another way to do itYou can also dump the contents of a String into a char[] with a String class method, and iterate that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String index out of range