Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   need help with StringTokenizer (http://www.velocityreviews.com/forums/t504416-need-help-with-stringtokenizer.html)

ros 05-06-2007 06:41 AM

need help with StringTokenizer
 
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


ligerdave 05-06-2007 07:22 AM

Re: need help with StringTokenizer
 
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();
}



ros 05-06-2007 08:28 AM

Re: need help with StringTokenizer
 
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



All times are GMT. The time now is 04:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.