Read data with delimater using stringtokenizer

Discussion in 'Java' started by Liang Yew, Jul 21, 2003.

  1. Liang Yew

    Liang Yew Guest

    Hei,
    I have problem with this
    if i have test with
    aa,bb,cc,dd
    aa,,cc,dd

    and use
    StringTokenizer str=new StringTokenizer(str,",",false)
    after i read the data will be as
    aa
    bb
    cc
    dd
    aa
    cc
    dd
    the blank data in row 2 is missing. any suggestion or any suggestion on
    method using beside stringtokenizer?

    Cheers,
     
    Liang Yew, Jul 21, 2003
    #1
    1. Advertising

  2. Liang Yew

    Lee Weiner Guest

    In article <bfffjp$7hg$>, "Liang Yew" <> wrote:
    >Hei,
    >I have problem with this
    >if i have test with
    >aa,bb,cc,dd
    >aa,,cc,dd
    >
    >and use
    >StringTokenizer str=new StringTokenizer(str,",",false)
    >after i read the data will be as
    >aa
    >bb
    >cc
    >dd
    >aa
    >cc
    >dd
    >the blank data in row 2 is missing. any suggestion or any suggestion on
    >method using beside stringtokenizer?


    If you're using JDK 1.4, use the String class split() method. It deals
    properly with the empty tokens.

    Lee Weiner
    lee AT leeweiner DOT org
     
    Lee Weiner, Jul 21, 2003
    #2
    1. Advertising

  3. Liang Yew

    Liang Yew Guest

    Thanks, Problem solve
    with
    public String[] split(String regex,int limit)

    cheers,

    "Liang Yew" <> wrote in message
    news:bffngg$eut$...
    Refer to the javasoft document,
    The string "b,,andf,," for example, yields the following results with these
    expressions:

    Regex Result
    , { "b", "", "andf" }

    my expected result it {"b","","andf","","")

    is that mean i must have space instead of "" of the raw data?

    Cheers

    "Lee Weiner" <> wrote in message
    news:q3ISa.96107$...
    In article <bfffjp$7hg$>, "Liang Yew" <>
    wrote:
    >Hei,
    >I have problem with this
    >if i have test with
    >aa,bb,cc,dd
    >aa,,cc,dd
    >
    >and use
    >StringTokenizer str=new StringTokenizer(str,",",false)
    >after i read the data will be as
    >aa
    >bb
    >cc
    >dd
    >aa
    >cc
    >dd
    >the blank data in row 2 is missing. any suggestion or any suggestion on
    >method using beside stringtokenizer?


    If you're using JDK 1.4, use the String class split() method. It deals
    properly with the empty tokens.

    Lee Weiner
    lee AT leeweiner DOT org
     
    Liang Yew, Jul 21, 2003
    #3
  4. Liang Yew

    Jacob Guest

    Liang Yew wrote:
    > Hei,
    > I have problem with this
    > if i have test with
    > aa,bb,cc,dd
    > aa,,cc,dd
    >
    > and use
    > StringTokenizer str=new StringTokenizer(str,",",false)
    > after i read the data will be as
    > aa
    > bb
    > cc
    > dd
    > aa
    > cc
    > dd
    > the blank data in row 2 is missing. any suggestion or any suggestion on
    > method using beside stringtokenizer?


    This is a common problem with StringTokenizer;
    It doesn't report empty tokens.

    The way to get around the problem is to use

    tokenizer = new StringTokenizer(strint,",",true)

    which will return the delimiters as well.
    Then you have enough information to deduce
    the empty tokens.

    It becomes somewhat messy anyway (as you need to
    remember the last token etc.) so encapsulating
    it in a StringTokenizer extended class is recommended.

    I regard it as a bug *not* including this an
    optional feature in the standard implementation.
     
    Jacob, Jul 21, 2003
    #4
    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:
    877
    Phil Hanna
    Jul 1, 2003
  2. ARMANDO  PADILLA

    StringTokenizer help

    ARMANDO PADILLA, Jul 4, 2003, in forum: Java
    Replies:
    3
    Views:
    616
    Chris Smith
    Jul 4, 2003
  3. Jim Crowell

    StringTokenizer Question?

    Jim Crowell, Aug 20, 2003, in forum: Java
    Replies:
    9
    Views:
    3,773
    Roedy Green
    Aug 23, 2003
  4. Newbie
    Replies:
    4
    Views:
    1,464
    Stefan Siegl
    Dec 7, 2003
  5. ruds
    Replies:
    5
    Views:
    551
    Randolf Richardson
    Jan 20, 2007
Loading...

Share This Page