Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Matching a string with a list of values

 
Karthik Krishnamurthy
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am in the midst of writing a validator. Basically, there are different tokens, each with multiple values. (For eg: HJ^23^SDF) with "^" being a delimiter separating the values inside a token.
I need to ensure that each of these values falls within an accepted set of values, i.e For Token HJ, the first value can be <23,45,24,76> etc.
I have about 20 tokens and between 3-8 values per token.
What would be the best way to do this.
Only thing I can think of is build multiple sets with the accepted values and check corresponding values with the appropriate set.
i.e
String[] pattern = {23,25,24,76}; Set set=new TreeSet();
for(i=0;i<pattern.length;i++)
set.add(pattern[i]);

And then
set.contains(value)

Is there a better/cleaner way to do this?
Thank You
Karthik
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that I understand the specification, but did you know that the String class has a method called split() that you can use to split a String when there is a single delimiter, such as "^"?
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, that should probably be "\\^", since ^ is a regex special character indicating the start of the string.
 
Karthik Krishnamurthy
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "^" is an example value. Usually the strings I get are of the type HJ*23*SDF. I would be extracting each value using a tokenizer on the *. I would then have to compare each value with a predefined accepted set of values as mentioned in the original post.
Is there any other way than what I have mentioned in the first post?

Thanks a lot
Karthik
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there are many possible ways, but if I understand your specification correctly, I think using sets is probably best. I would probably use HashSets rather than TreeSets, unless there's some reason you need the set contents to be sorted. If you're just using contains, HashSet is usually faster. I would also use Kaydell's suggestion with String.split(). Note that "*" is another special character in regular expressions, which also requires escaping:

It's also possible to construct a single regular expression for each token, that lists the allowed values. However this is probably hard to read, as well as less efficient. It woud look something like this:

Although I'm not sure it's really less efficient. You can always try it both ways and see. Probably I would use whichever of those two looks easier for you to understand. Some people know regular expressions very well, in which case the regex solution may be good. In general though I would favor the HashSet solution.
[ February 25, 2008: Message edited by: Jim Yingst ]
 
Karthik Krishnamurthy
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys. I'll go with the hash set way. Only things is theres about 100 different types of elements each with about 4-5 different values. Adding them to the sets if going to be a pain!

Thanks again.

Karthik
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm... can't you use loops for that sort of thing?

It's a little longer if you don't have JDK 5 or 6, but the same idea applies - most of the work can be done in a method that you can reuse for each set.
[ February 25, 2008: Message edited by: Jim Yingst ]
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My instinct would be to declare a static Pattern for each token. I know that regular expressions are not familiar to most developers, but as they can be so useful it is well worth the effort to learn about this subject.
 
Karthik Krishnamurthy
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the inputs guys. I think I will go with looping for now, studying Patterns on the side for the second phase of this project.

Thanks again
Karthik
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic