Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help using StringTokenizer

 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A loop of some kind (for or while) is necessary, which runs from 1 to numb-1.
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome. Well done sorting it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic