This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes need help using StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help using StringTokenizer" Watch "need help using StringTokenizer" New topic
Author

need help using StringTokenizer

Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Hello,

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: 41592
    
  55
A loop of some kind (for or while) is necessary, which runs from 1 to numb-1.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
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
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
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
You are welcome. Well done sorting it out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help using StringTokenizer