Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing Dat file with servlets !!

 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys !
I am writing a servlet which parses a .dat file (which has records seperated by "~" as a delimiter) and then loads them into a database.I am using StringTokenizer class for parsing. Everything goes well if the Dat file has some value between two successive delimiters in each row and all the rows. But if there is an instance where to delimiters are together (eg. " ....~~...") then it throws NoSuchElementFoundException. I can't think of any other way of doing this. Please suggest.
ThanX
Manoj
 
Darrell Henry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj,
The only successful way I've found to deal with this is to write code to "examine" the line before you attempt to use any of its contents for populating a database (someone correct me if I'm wrong).
It's long and tedious, I know - and not pretty! See my earlier post. I'm doing something like this: look at the "DoPost" pasted in code.
Basically, you will need to find the position of each delimiter in your line (String.indexOf('~')), use the return value as an offset while locating the next delimiter. Based on the index position of the next found delimiter, you should be able to determine if there was something between the two (because you know the position of the first!).
If the results indicate that no value exists between your delimiters, then you can actually set the variable that was supposed to hold the returned value to "null" or some other appropriate value.
Let me know if this makes sense.
Regards,
- Darrell
 
sanjay yermalkar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had this problem.
I used this constructor of the StringTokenizer:
StringTokenizer(String str, String delim, boolean returnDelims)
with returnDelims = true.
Now, the nextToken() method will return delimiter as a token.
So,
String s = "a~b~~c"
will be tokenized as:
a
~
b
~
~
c
Thus, with a simple algorithm you can find out when you had two successive delimiters.


[This message has been edited by sanjay yermalkar (edited September 20, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!