but my "(a>b)||(c==d)&&(z==1)" is an expression in string format.
i found another way of doing at
http://forums.sun.com/thre
Main Topics
Browse All Topicshi,
i am trying to evaluate a given if() statement. but the problem is the given if() is a String.
Example.
String someIF = "if((a>b)||(c==d)&&(z==1))
how can i evaluate someIF and extracting the different variables needed for evaluation?
thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
but my "(a>b)||(c==d)&&(z==1)" is an expression in string format.
i found another way of doing at
http://forums.sun.com/thre
The solution depends a lot on the exact grammar of the allowed expressions. If your expressions are allowed to be real Java expressions, then you're looking at a very non-trivial requirement.
On the other hand, if your expressions are only allowed to look like the expression in your example, then you could write your own custom parser for composite boolean expression consisting of simple ones involving no more than variables and constants (note: no sub-expressions, or method calls!).
Check out StringTokenizer.. a very old class that has been reused many times to do the latter.
Another approach that would be possible in Java 1.6+ environments is to wrap the expression in a compilable method harnass, itself wrapped by a class body.. then use the compilation facilities to compile this generated class. Then.. you load this class (dynamic loading), passing your variables' values to your class/method, and have Java itself evaluate the expression.
So your synthetic class would have to look something lke
public class BooleanExpressionEvaluator
public static boolean eval(int a, int b, int c, int d, int z) {
return (a>b) || (c==d) && (z==1);
}
}
This approach would be a fair bit of work too.. but it would support much more complex expressions.
I've got the strong feeling you are a script programmer trying to do some Java :) Welcome to the world without eval("code"). You can circumvent things like these using the already proposed solution, or by using things like bsh, which provide a scripting platform. Just creating a expression from a string and using it directly within a method body is not an option in Java. You might also want to check out the latest scripting support in Java (where you can use Java Script within Java applications).
Business Accounts
Answer for Membership
by: PrasathAnnamalaiPosted on 2008-08-21 at 21:30:08ID: 22286698
you can write like this
String someIF = (a>b)||(c==d)&&(z==1)? "success": "Failure";
Select allOpen in new window