StringBuffer Class

An instance of Java's StringBuffer class represents a string that can be dynamically modified. These are the constructors:

StringBuffer() This constructs an empty string buffer
StringBuffer(int capacity) This constructs an empty string buffer with the specified initial capacity.
StringBuffer(String initialString)This constructs a string buffer that initially contains the specified string.


StringBuffer Methods:
StringBuffer append(String str)This appends str to the current string buffer. Alternative forms support appending primitives and character arrays; these are converted to strings before appending.
StringBuffer append(Object obj)This calls toString() on obj and appends the result to the current string buffer.
StringBuffer insert(int offset, String str)This inserts str into the current string buffer at position offset. There are numerous alternative forms.
StringBuffer reverse()This reverses the characters of the current string buffer.
StringBuffer setCharAt(int offset, char newchar)This replaces the character at position offset with newchar.
StringBuffer setLengthInit newLength)This sets the length of the string buffer to newLength. If newLength is less than the current length, the string is truncated. If newLength is greater than the current length, the string is padded with null characters.

StringBuffer In Action: Here's a short example program showing a few StringBuffer methods in action:


Sample Assignment: This might be a difficult project. You will create an application which scrambles words. The user will enter a word as an argument and indicate a scrambling method. The easiest way to indicate a scrambling method is with a 1, 2, or 3 since there are just three methods.

Here are the three methods you must implement for this assignment:
MethodBrief DescriptionExample
1reverse, store, truncate, insert input: carpet
step one: teprac
step two: store last half of word (rac)
step three: truncate last half of word
step four: insert stored part (rac) infront of first part (tep)
result: ractep
2pick random letter and alternately insert in front of it and concat after it input: carpet
step one: pick random letter: (a) store it and delete it from input
step two: insert first letter from input infront of stored letter: ca
step three: concatenate next letter at end: car
repeat insert/concatenate: pcar, pcare, tpcare.
result: tpcare (actual results will vary since first letter is selected at random).
3store, truncate, random insert four times input: carpet
step one: store last letter (t), truncate last letter, randomly insert
possible result to step one: catrpe
step two: store last letter (e), truncate last letter, randomly insert
possible result to step two: caetrp
step three: store last letter (p), truncate last letter, randomly insert
possible result to step three: caetpr
step four: store last letter (r), truncate last letter, randomly insert
possible result to step four: caretp
result: caretp (actual results will vary since the four insertions are made into randomly selected positions)

Basically, you will have a case statement which is used to route a call to one of these three methods. Each of these methods must return a String.