This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread 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 Murach's Java Servlets and JSP this week in the Servlets 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: 41062
    
  43
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: 37926
    
  22
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: 37926
    
  22
You are welcome. Well done sorting it out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: need help using StringTokenizer
 
Similar Threads
Extracting integers from a string.
I am supposed to call two service classes from one client(main) class?
B&S public int[] find(String[] criteria)
HELP!!!! how does Character.isLetter work? (need experts)
I am supposed to call two service classes from one client(main) class?