I need help writing code that converts a given name like into initials and surname. For example, if a name is Ricky James, it should do R James and if the name is Henry William James, that should become H W James.
I have written a method that converts Ricky James to R James and I have used StringTokenizer, but I need help if the name has 3 or more tokens, like the second one.
I know I can use StringTokenizer but I can't really make out how the logic works.
My try is pasted below:
Would really appreciate help with this. Ricky James
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
0
A loop of some kind (for or while) is necessary, which runs from 1 to numb-1.
Go through the StringTokenizer API, where it says that it is deprecated, and you should use String.split() instead. String.split() returns a String[] array, which you may find easier to use. [edit]minor spellling corrections[/edit] [ May 06, 2007: Message edited by: Campbell Ritchie ]
Ricky James
Ranch Hand
Joined: Mar 26, 2007
Posts: 83
posted
0
Originally posted by Ulf Dittmer: A loop of some kind (for or while) is necessary, which runs from 1 to numb-1.
Thanks for the tip Ulf. I solved the problem using while loop. I am pasting the code below for the benefit of other beginners.
Appreciate your help.
Cheers Ricky
Ricky James
Ranch Hand
Joined: Mar 26, 2007
Posts: 83
posted
0
Originally posted by Campbell Ritchie: Go through the StringTokenizer API, where it says that it is deprecated, and you should use String.split() instead. String.split() returns a String[] array, which you may find easier to use. [edit]minor spellling corrections[/edit]
[ May 06, 2007: Message edited by: Campbell Ritchie ]
Thanks for this tip Ritchie. I have solved the problem using StringTokenizer and have pasted the code of the method in the previous method. But as a next step I am gonna use String.split() to learn that.
Really appreciate you took time to reply to my post. Ricky