Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about StringTokenizer !!

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4028
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic