StringTokenizer help

Discussion in 'Java' started by ARMANDO PADILLA, Jul 4, 2003.

  1. Hello Everyone I have a quick and simple problem that i cant seem to get
    around. Im reading a file and seperating the fields from the file via the
    delimeter ",". The problem arises when the nextToken() encounteres a blank
    field. Is there a way to go around it and have the process ignor the null
    field value?

    For example:

    String: Armando, padilla, 0, 12, 3421, hello there
    Another, Name, , 32142, 1, hello

    The first line will parse fine but the second will issue out an error.

    Thanks for the help.

    --
    Armando P.
    Computer Science/Economics, Cailifornia State University Los Angeles
    http://www.armando.ws
     
    ARMANDO PADILLA, Jul 4, 2003
    #1
    1. Advertising

  2. "ARMANDO PADILLA" <> wrote in message
    news:YR7Na.645$...
    > I have a quick and simple problem that i cant seem to get around. Im
    > reading a file and seperating the fields from the file via the delimeter
    > ",". The problem arises when the nextToken() encounteres a blank field.
    > Is there a way to go around it and have the process ignor the null field
    > value?
    >
    > For example:
    >
    > String: Armando, padilla, 0, 12, 3421, hello there
    > Another, Name, , 32142, 1, hello
    >
    > The first line will parse fine but the second will issue out an error.


    I think we need more information. If the delimiter string is ",", then the
    second line's third token will be a single space. In what way is this
    causing you a problem? Are you getting an exception because you're trying
    to convert the space to an int? What do you normally do with the third
    token on a line, and what do you want to happen in this special case?

    It might help if you illustrate your problem with a small, stripped-down
    version of your code.

    --
    Russell Hanneken
     
    Russell Hanneken, Jul 4, 2003
    #2
    1. Advertising

  3. ARMANDO  PADILLA

    Jacob Guest

    ARMANDO PADILLA wrote:
    > Hello Everyone I have a quick and simple problem that i cant seem to get
    > around. Im reading a file and seperating the fields from the file via the
    > delimeter ",". The problem arises when the nextToken() encounteres a blank
    > field. Is there a way to go around it and have the process ignor the null
    > field value?
    >
    > For example:
    >
    > String: Armando, padilla, 0, 12, 3421, hello there
    > Another, Name, , 32142, 1, hello
    >
    > The first line will parse fine but the second will issue out an error.


    StringTokenizer *do* ignore empty fields, but
    in your case it is not empty: A " " is returned.

    Actually it is rather annoying that null is not
    returned in the case of subsequent delimiters,
    at least optionally:
    a,,b,,c should give "a", null, "b", null, "c".
    As it is now you'll have to ask for the delimiters
    to be returned as well and figure this out
    manually.
     
    Jacob, Jul 4, 2003
    #3
  4. ARMANDO  PADILLA

    Chris Smith Guest

    Jacob wrote:
    > StringTokenizer *do* ignore empty fields, but
    > in your case it is not empty: A " " is returned.
    >
    > Actually it is rather annoying that null is not
    > returned in the case of subsequent delimiters,
    > at least optionally:
    > a,,b,,c should give "a", null, "b", null, "c".


    I'm going into picky mode here, but this is a real pain when it's done
    wrong. I agree that StringTokenizer should optionally indicate empty
    fields... but null is NOT the right return value. There is a big
    difference between null and the empty string (""). The empty field
    should be indicated by an empty string, not a null. When someone isn't
    thinking and gets this wrong, it just becomes a constant pain in the
    side of people trying to use the poorly designed API.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 4, 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:
    879
    Phil Hanna
    Jul 1, 2003
  2. Liang Yew
    Replies:
    3
    Views:
    656
    Jacob
    Jul 21, 2003
  3. Jim Crowell

    StringTokenizer Question?

    Jim Crowell, Aug 20, 2003, in forum: Java
    Replies:
    9
    Views:
    3,784
    Roedy Green
    Aug 23, 2003
  4. ros
    Replies:
    2
    Views:
    469
  5. ronron10

    Help with java StringTokenizer

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

Share This Page