This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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
 
Similar Threads
OOCalculator >> Sidebar >> Mike Matola
String conversion
StringBuffers and Tokenizers
How to read a .txt file and open its contents
Getting certain information out of a .csv file