Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String index out of range

 
Juan Villena
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Grayson Churchel
Greenhorn
Posts: 7
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
i just did it and it works

 
Grayson Churchel
Greenhorn
Posts: 7
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy to help.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do itYou can also dump the contents of a String into a char[] with a String class method, and iterate that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic