Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String tokenizing- Advise needed

 
John Mattman
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Java smarts,
I have code string arrray that has names(including first and last name and Mr, Mrs, Jr & etc) in it.

Here is an example.

String[] names = {"Bobby Bounds", "Dawn & Gary McMullin", "Mr. & Mrs. Rick Spears", "Mr John E. & Gary R. Scott"};


What is the best way to parse the first and last names out of it.
I know lot of people are very smart in this forum. Let me know your inputs please?

Thank you,
Matmann
 
Amir Pourteymour
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass your string to a StringTokenizer, and push each token into a stack. Then start reading characters from the stack, and as soon as you see a "&", you can separate them.


 
John Mattman
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advise. I got it.
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use StringTokenizer. It is a legacy class and really has little value, other than to support legacy code.

You can use String.split() and with a carefully created regular expression you can do this with a little code.
[ March 08, 2008: Message edited by: Robert Hill ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mattman, you will have to change your screen name to conform with local policy, they will make you do this. Java Ranch's statement on the issue says they tried to allow this but it just gets to be too much joking and not enough work.

If you change it soon enought there will be no problem.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64850
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Mattman", please check your private messages for an important administrative matter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic