This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringIndexOut OfBoundsException" Watch "StringIndexOut OfBoundsException" New topic
Author

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: 2367
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


cheers
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: StringIndexOut OfBoundsException