Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Read data with delimater using stringtokenizer

Reply
Thread Tools

Read data with delimater using stringtokenizer

 
 
Liang Yew
Guest
Posts: n/a
 
      07-21-2003
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,



 
Reply With Quote
 
 
 
 
Lee Weiner
Guest
Posts: n/a
 
      07-21-2003
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
 
Reply With Quote
 
 
 
 
Liang Yew
Guest
Posts: n/a
 
      07-21-2003
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$ et...
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



 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      07-21-2003
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.







 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting fields from file using StringTokenizer. ruds Java 5 01-20-2007 07:32 AM
StringTokenizer insert into Array?? Newbie Java 4 12-07-2003 06:19 PM
StringTokenizer Question? Jim Crowell Java 9 08-23-2003 06:28 PM
StringTokenizer help ARMANDO PADILLA Java 3 07-04-2003 04:37 PM
Re: StringTokenizer Help Phil Hanna Java 0 06-30-2003 11:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57