• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringTokenizer and consecutive delimiters  RSS feed

 
Frederick Clark
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I used a StringTokenizer to divide the fields of a String read from a text file. When it gets consecutive delimiters, || for example, I'd like to get a field with a null inside it. The StringTokenizer simply branches consecutive delimiters and don't read this field.
How can I deal with this?
Thank you
 
Mark Bensing
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass a boolean to the StringTokenizer conscructor to have it return the delimiters as tokens. It will require a little more code to recognize and ignore the delimiters but it allows you to check for multiple delimiters.
I hope this helps,
Mark
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ultimately I think you'll be much better off using either String's split() method, or the java.util.regex package (which is used internally by split() anyway). E.g.

Note that the argument to split() is a regular expression, so you may need to use escape sequences for some characters. E.g. for "." use "\\." instead. Learning the rules for regular expressions will help you out an many ways later on, so it's worthwhile to study them now. See the documentation for Pattern for more info.
 
Frederick Clark
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I though about passing a true argument to the constructor and managing all the tokens delimiters, but what I'm looking for is a way to reduce coding, and this just increase it. Thanks anyway, Mark. The second option, Jim, seems to be promiser, but think is only available in the release 1.4. I'm using 1.3 in my development for compatilibity reasons. Is possible to use Java 1.4 classes with 1.3?
 
Frederick Clark
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, I used your approach, using the split() method in the Java 1.4 String class and it works. I'll find a way to use this class from the 1.3 VM
 
Frederick Clark
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frederick - I think you'll have a hard time using 1.4 classes from 1.3. Even if you manage to configure your own environment for this, it's going to be really difficult to port the code to any other JVM. Try looking at the solutions in this thread instead.
Note that all these solutions - split() and the two solutions by Steve Deadsea and myself - return an empty string "" rather than null, for the case where there are two consecutive delimiters.
[ April 15, 2003: Message edited by: Jim Yingst ]
 
Frederick Clark
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, although I'm asking for a solution, I'd previously solved this by creating a new StringTokenizer, but I doesn't work so well (low performance). I think I'm going to use your StrTokenizer class.
Thank you.
I also have a trouble with with inquiring disk info, and want to know if there's a way to deal with this without using JNI. I posted a Message with this topic
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!