aspose file tools*
The moose likes Beginning Java and the fly likes String index out of range exception 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 exception" Watch "String index out of range exception" New topic
Author

String index out of range exception

Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
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

Joined: Aug 31, 2004
Posts: 11343

If the length of a String is 60, then what is charAt(60)?

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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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).


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

(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

Joined: Oct 27, 2005
Posts: 19718
    
  20

And unlike C strings (char arrays), Java strings are NOT NULL terminated (\0).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
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

Joined: Jun 15, 2008
Posts: 11
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

Joined: Oct 02, 2003
Posts: 11398
    
  16

is there a reason why you can't do something like this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Originally posted by fred rosenberger:
is there a reason why you can't do something like this:




Thanks for the reply. Will use that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String index out of range exception