need help with StringTokenizer

Discussion in 'Java' started by ros, May 6, 2007.

  1. ros

    ros Guest

    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
    ros, May 6, 2007
    #1
    1. Advertising

  2. ros

    ligerdave Guest

    On May 6, 1:41 am, ros <> 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();
    }
    ligerdave, May 6, 2007
    #2
    1. Advertising

  3. ros

    ros Guest

    On May 6, 12:22 am, ligerdave <> wrote:
    > On May 6, 1:41 am, ros <> 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
    ros, May 6, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Hanna

    Re: StringTokenizer Help

    Phil Hanna, Jul 1, 2003, in forum: Java
    Replies:
    0
    Views:
    856
    Phil Hanna
    Jul 1, 2003
  2. ARMANDO  PADILLA

    StringTokenizer help

    ARMANDO PADILLA, Jul 4, 2003, in forum: Java
    Replies:
    3
    Views:
    599
    Chris Smith
    Jul 4, 2003
  3. Liang Yew
    Replies:
    3
    Views:
    632
    Jacob
    Jul 21, 2003
  4. Jim Crowell

    StringTokenizer Question?

    Jim Crowell, Aug 20, 2003, in forum: Java
    Replies:
    9
    Views:
    3,740
    Roedy Green
    Aug 23, 2003
  5. ronron10

    Help with java StringTokenizer

    ronron10, Dec 18, 2008, in forum: Java
    Replies:
    0
    Views:
    393
    ronron10
    Dec 18, 2008
Loading...

Share This Page