aspose file tools*
The moose likes Beginning Java and the fly likes StringBuffer toString method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringBuffer toString method" Watch "StringBuffer toString method" New topic
Author

StringBuffer toString method

Jimmy Anderson
Greenhorn

Joined: Aug 22, 2003
Posts: 3
Hi all,
I am working through the Java Tutorial third edition and one of the exercises is to write a program that computes your initials from your full name and displays them. I set the app up to accept the full name from the command line. My thought was to first append the first letter of the string to a StringBuffer then iterate through the string and append the character after a blank space to the StringBuffer. The output only prints the first letter of the string though. After some experimentation it seems that when I use the toString method on the StringBuffer it removes the whitespaces so the loop never finds a blank space. Can any explain why this is and how can I get around it?
Thanks in advance!! Here's my code:
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Jimmy -
Arguments are separated by whitespace, but the whitespace is not part of the argument. Running java Initials Jeffrey Scott Bosch produces the following elements of the args array:
args[0] = "Jeffrey"
args[1] = "Scott"
args[2] = "Bosch"
Concatentation would produce JeffreyScottBosch. You could append a space after adding the element if the element index is less than (args.length - 2):

Now you have a space appended after every name in the StringBuffer except the last name.

Note: I would also use a for loop instead of the while, but that's just my preference.

Regards,
Jeff


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Why do you concatenate the arguments at all, just to separate them again?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Richard Teston
Ranch Hand

Joined: Feb 12, 2002
Posts: 89
Hi,
yeah, i agree with Ilja why not get every initial character with every elements of String in the command line arguments?


The Code is the Programmer
Jimmy Anderson
Greenhorn

Joined: Aug 22, 2003
Posts: 3
Thanks for your responses.
For some reason I was thinking that the command line was returning one String object instead of an array with three String elements. Your responses made me realize my mistake and I have revised the program. It works fine now. Thanks again for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer toString method