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:
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 = "Jeffrey" args = "Scott" args = "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.
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...)
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
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
Joined: Aug 22, 2003
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com