Read data with delimater using stringtokenizer

L

Liang Yew

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,
 
L

Lee Weiner

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
 
L

Liang Yew

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

cheers,

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

"Liang Yew" <[email protected]> said:
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
 
J

Jacob

Liang said:
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.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top