StringTokenizer help

  • Thread starter ARMANDO PADILLA
  • Start date
A

ARMANDO PADILLA

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.
 
R

Russell Hanneken

ARMANDO PADILLA said:
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.
 
J

Jacob

ARMANDO said:
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.
 
C

Chris Smith

Jacob said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top