It's not a secret anymore!
The moose likes Beginning Java and the fly likes StringIndexOut OfBoundsException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringIndexOut OfBoundsException" Watch "StringIndexOut OfBoundsException" New topic

StringIndexOut OfBoundsException

Nikos Stavros
Ranch Hand

Joined: Feb 24, 2006
Posts: 243

I get this error "StringIndexOutOfBoundsException" when my input string is st= "Hold State,0,0,0,0,0,0,0,0"

The code below shows what I'm trying to do, create a StingBuffer os size 8 from the input

any ideas?

Jesus lives
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
When you use the constructor of StringBuffer that takes an int parameter, it does not create a StringBuffer with that many characters. It creates a StringBuffer with that capacity.

The StringBuffer will have no characters in it.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi Nikos,

Are you trying to split obtained the contents of the comma-delimited String? Read the Java API and you'd notice that StringTokenizer is not recommended for usage anymore, except in supporting legacy codes.

As of JDK 1.4 and later, you could use the split method of String class.

This way, your codes is easier to maintain & understand. There's also performance gain involved, if I'm not mistaken.

SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Nikos Stavros
Ranch Hand

Joined: Feb 24, 2006
Posts: 243

Thanks, it was helpful

it now works

I agree. Here's the link:
subject: StringIndexOut OfBoundsException
It's not a secret anymore!