jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Doubt about StringTokenizer !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt about StringTokenizer !!" Watch "Doubt about StringTokenizer !!" New topic

Doubt about StringTokenizer !!

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Hello Everybody.

I am using StringTokenizer to read a CSV(uploaded) file and write it into a file.....

My problem is, i want to allow user to upload a CSV file which has got only 19 colunms in it. Not less than or more than 19 columns. I don't how how to do that. I am pasting code here

Can anyone help me this regards, Please?

Thanks for your time.

With Regards,
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
I suggest you use methods from the the StringUtils class http://jakarta.apache.org/commons/lang/api/index.html.

It's got useful methods for checking the number of tokens, getting a string array of the tokens etc. Much more useful than the raw StringTokenizer.

ASCII silly question, Get a silly ANSI.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

Your code makes the excellent argument of why you should use an array of objects instead of variables... you could seriously cut the number of lines of code and use that to iterate on instead of listing everything one at a time.

I think what you really want to use is java.lang.String.spilt() which given a regular expression splits the entry into an array of elements based on the regular expression. Then, you would just have to check that the array created by split has exactly 17 elements.

But still, I think the first step is to get rid of your long list of variables then have an array. If you prefer String Tokenizer (which I don't recommend here) then you could just iterate on the elements calling hasMoreTokens() repeatedly checking keeping a count of how many elements were found. Its *ALWAYS* a good idea to call StringTokenizer.hasMoreTokens() before calling StringTokenizer.nextToken().
[ November 13, 2005: Message edited by: Scott Selikoff ]

[OCA 8 Book] [Blog]
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Further, though unasked for, I suggest you use an array instead of declaring 20 - 30 variables like you have. That way, your code won't break if you need to change the number of tokens tomorrow.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Alternatively, instead of writing your own parser, use an existing one, and check how many column it has read. That one also the benefit of not choking on escaped characters, which yours would.
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
thanks Ash,
I wanted to know, how to import this StringUtils class? My program is giving some error as "Unable to compile class JSP import java.StringUtils.*;
Can you please help me in this regard ???

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Thanks to everyone.
I am using seperate variable because, my programs requires it....Otherwise i would have considered your idea of using iteration and array.

I am using "StringTokenizer.hasMoreTokens()" before using "StringTokenizer.nextToken()"

To use Split() method, which class should i import? I am implementing all these in a JSP page.

Please guide me.
I agree. Here's the link: http://aspose.com/file-tools
subject: Doubt about StringTokenizer !!
It's not a secret anymore!