wood burning stoves 2.0*
The moose likes Performance and the fly likes Strings - design issue  for validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Strings - design issue  for validation" Watch "Strings - design issue  for validation" New topic

Strings - design issue for validation

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Hi all,
I have a strange requirement.

I have a String like: Meals(veg(soup&starters(price=40 || 50))nonveg(seaFood)delivery=parcel || ready_to_eat)

I parse this string and put all the values with in veg to a string array

String[] veg = {soup,starters}
String[] nonveg = {seafood}
String[] delivery={parcel,ready_to_eat}

Now, my question is:
I need to validate these values.
Meals = it should be validated against uppercase or lowercase, and correct spelling. if it fails, no need to validate other stuff.

If Meals is validated succssfully, I need to validate veg in such a way that:
veg should have only soup and starters with in that. nothing else.
if it has veg(salad) then, the validation should fails and send "fail" as response.

Is there a best design to do this ? My desing as follows:

My problem is how to call the methods or the sequence of methods? so that the complete Order String is validated.
do i need to put the method calls in if...else or in switch statement or is there any other way? I want the best desing for this issue.

Please help me?

With Regards,
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
Looks like a computer science assignment for language tokenizing and syntax analysis.

This task will be broken down to at least 2 steps : tokenizing and syntax-analyzing


String[] tokens = tokenize(inputString);

boolean valid = analyzeSyntax(tokens);

tokenize() will break down the string into chunks according to your language.

analyzeSyntax() will check the chunks 1 by 1, if it complies to your language's rules. I have implemented this using recursion.
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Thank you for your reply.
I have already done with the coding of analyzing the string and putting it into different String Array.
My problem is for Validation. How to validate each segment ?

Price is with in Veg Segment. Veg Segment is with in Meals.
So, first i need to validate the Meals. IF validation fails there, no need to validate other items.
If validation for Meals is success, Then it should move to veg segment to validate. how to do this....I want to way to call the validation process......
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 7554

Anu satya wrote:If validation for Meals is success, Then it should move to veg segment to validate. how to do this....I want to way to call the validation process......

Seems to me that what you had looked fine, unless you want to encode your "rules" somehow.

The only suggestion I'd have is for your validation methods to return a boolean indicating whether they worked or not, viz:You can't use a switch statement because each stage depends on the outcome of a previous one.

[Edit] An alternative, if there's no other logic involved, is simply to string them together:because && guarantees that the RHO is only evaluted if the LHO is true.
However it's all dependant on your validation methods returning a result.



Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12761
Note that if your problem got too complex to handle with your programmed logic approach, you could graduate to a Business Rules Engine.

It is sorta covered in the JavaRanch Style Guide.
subject: Strings - design issue for validation
Similar Threads
Will you be willling to be a veggie if it helps you calm you down?
what pattern is this.....?
Approach to validate objects in list
Assignment Help
Validation and copying