Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > need help with StringTokenizer

Reply
Thread Tools

need help with StringTokenizer

 
 
ros
Guest
Posts: n/a
 
      05-06-2007
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 method hasMoreTokens() but I can't
really make out how the logic works.

My try is pasted below:

code:

void myMethod(String name) {

this.name = name;
StringTokenizer str = new StringTokenizer(name, " ");
int numb = str.countTokens();
String first = str.nextToken();
String second = str.nextToken();
System.out.println("The no. of tokens are - " + numb);
System.out.println("The name is - " + first.charAt(0) + "
" + second);

}



Would really appreciate help with this.
Ros

 
Reply With Quote
 
 
 
 
ligerdave
Guest
Posts: n/a
 
      05-06-2007
On May 6, 1:41 am, ros <ros...@gmail.com> wrote:
> 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 method hasMoreTokens() but I can't
> really make out how the logic works.
>
> My try is pasted below:
>
> code:
>
> void myMethod(String name) {
>
> this.name = name;
> StringTokenizer str = new StringTokenizer(name, " ");
> int numb = str.countTokens();
> String first = str.nextToken();
> String second = str.nextToken();
> System.out.println("The no. of tokens are - " + numb);
> System.out.println("The name is - " + first.charAt(0) + "
> " + second);
>
> }
>
> Would really appreciate help with this.
> Ros



check countTokens() every time after you used nextToken() to see if it
returns 1. if it returns 1, you know there is only one token left.

something like this:

StringTokenizer str = new StringTokenizer(name);

while(str.hasMoreTokens()){
if(str.countTokens() > 1)
str.nextToken().charAt(0);
else
str.nextToken();
}


 
Reply With Quote
 
 
 
 
ros
Guest
Posts: n/a
 
      05-06-2007
On May 6, 12:22 am, ligerdave <david.c...@gmail.com> wrote:
> On May 6, 1:41 am, ros <ros...@gmail.com> wrote:
>
>
>
> > 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 method hasMoreTokens() but I can't
> > really make out how the logic works.

>
> > My try is pasted below:

>
> > code:

>
> > void myMethod(String name) {

>
> > this.name = name;
> > StringTokenizer str = new StringTokenizer(name, " ");
> > int numb = str.countTokens();
> > String first = str.nextToken();
> > String second = str.nextToken();
> > System.out.println("The no. of tokens are - " + numb);
> > System.out.println("The name is - " + first.charAt(0) + "
> > " + second);

>
> > }

>
> > Would really appreciate help with this.
> > Ros

>
> check countTokens() every time after you used nextToken() to see if it
> returns 1. if it returns 1, you know there is only one token left.
>
> something like this:
>
> StringTokenizer str = new StringTokenizer(name);
>
> while(str.hasMoreTokens()){
> if(str.countTokens() > 1)
> str.nextToken().charAt(0);
> else
> str.nextToken();
> }


Thanks for the advice ligerdave. That solved my problem.

Really appreciate your help.

Ros

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with java StringTokenizer ronron10 Java 0 12-18-2008 02:54 AM
StringTokenizer Question? Jim Crowell Java 9 08-23-2003 06:28 PM
Read data with delimater using stringtokenizer Liang Yew Java 3 07-21-2003 07:10 AM
StringTokenizer help ARMANDO PADILLA Java 3 07-04-2003 04:37 PM
Re: StringTokenizer Help Phil Hanna Java 0 06-30-2003 11:57 PM



Advertisments