| Read data with delimater using stringtokenizer |
|
| Author |
Message |
Liang Yew

|
Posted: 7/21/2003 9:38:00 AM |
Top |
java-programmer, Read data with delimater using stringtokenizer
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,
|
| |
|
| |
 |
lee

|
Posted: 7/21/2003 10:18:00 AM |
Top |
java-programmer >> Read data with delimater using stringtokenizer
In article <bfffjp$7hg$email***@***.com>, "Liang Yew" <email***@***.com> 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

|
Posted: 7/21/2003 12:09:00 PM |
Top |
java-programmer >> Read data with delimater using stringtokenizer
Thanks, Problem solve
with
public String[] split(String regex,int limit)
cheers,
"Liang Yew" <email***@***.com> wrote in message
news:bffngg$eut$email***@***.com...
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" <email***@***.com> wrote in message
news:q3ISa.96107$email***@***.com...
In article <bfffjp$7hg$email***@***.com>, "Liang Yew" <email***@***.com>
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
|
| |
|
| |
 |
Jacob

|
Posted: 7/21/2003 3:10:00 PM |
Top |
java-programmer >> Read data with delimater using stringtokenizer
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.
|
| |
|
| |
 |
| |
|