Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String index out of range exception

 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All helpful people, I need to take your help here.

I have a String object and an integer array

For the length of the string, I am trying to do something like this. My code is as follows..



I keep getting



Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 60
at java.lang.String.charAt(String.java:558)
at <CLASSNAME>.main(<CLASSNAME>.java:9)



Help would be greatly appreciated.

Thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the length of a String is 60, then what is charAt(60)?

Hint: The first char is charAt(0) -- not charAt(1).
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
If the length of a String is 60, then what is charAt(60)?

Hint: The first char is charAt(0) -- not charAt(1).


Hi, Thanks for the reply. I am not quite understanding what you are saying here..

charAt(60) will be the last character in the string.

Please explain your point here
 
Henry Wong
author
Marshal
Pie
Posts: 21126
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
charAt(60) will be the last character in the string.


Let's try this one again....

charAt(0) will be the first character in the string.
charAt(1) will be the second character in the string.
charAt(2) will be the third character in the string.

Work this all the way to charAt(60) please.

Henry
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point marc is trying to make is that String indexes start at 0. Therefore for a nonempty String of length x, the first character is always at index 0, the last character is always at index (x - 1).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Avi Sridhar:
...charAt(60) will be the last character in the string...

No.

The 1st char is not charAt(1). It is charAt(0).
The 2nd char is not charAt(2). It is charAt(1).
The 3rd char is not charAt(3). It is charAt(2).
And so on.

So if the String has 60 chars, the 60th char is not charAt(60).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Whoops... Posted before I saw Henry's and Garrett's responses. So it's redundant, but hopefully clear now.)
[ June 20, 2008: Message edited by: marc weber ]
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And unlike C strings (char arrays), Java strings are NOT NULL terminated (\0).
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
And unlike C strings (char arrays), Java strings are NOT NULL terminated (\0).


Thanks everyone for your thoughts. it really helped.

I am going to use this instead



and we will get 60 chars there.
 
Paul Sisco
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Avi Sridhar:


Thanks everyone for your thoughts. it really helped.

I am going to use this instead



and we will get 60 chars there.


This isn't exactly about your question. Hopefully I am not out of place asking this...
Are you sure the string length will always be 60? you should probably use a variable for the string length in place of the number 60. This way you can handle any length string up to the limit of your int array.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a reason why you can't do something like this:

 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
is there a reason why you can't do something like this:




Thanks for the reply. Will use that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic