File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes String tokenizing- Advise needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String tokenizing- Advise needed" Watch "String tokenizing- Advise needed" New topic

String tokenizing- Advise needed

John Mattman
Ranch Hand

Joined: Feb 18, 2008
Posts: 45
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,
Amir Pourteymour
Ranch Hand

Joined: Mar 06, 2008
Posts: 45
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

Joined: Feb 18, 2008
Posts: 45
Thanks for the advise. I got it.
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
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

Joined: Sep 17, 2006
Posts: 1282
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.

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63857

"Mattman", please check your private messages for an important administrative matter.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: String tokenizing- Advise needed
It's not a secret anymore!