• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Doubt about StringTokenizer !!

 
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.
 
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.
 
author
Posts: 4173
29
jQuery Eclipse IDE Java
  • 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.
 
Rancher
Posts: 43015
76
  • 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.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic