Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String index out of range

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 7
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happy to help.
 
Marshal
Posts: 79469
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic