6

I am trying to evaluate the following from a string

boolean value = evaluate("false || true && true && false || true");

I need to get a boolean value of true for this one.
Any ideas on how to solve this problem in the most efficient way?

5
  • 3
    look at the question: stackoverflow.com/questions/2605032/using-eval-in-java Commented Jun 4, 2010 at 13:10
  • thanx Thierry, will take a look now. Commented Jun 4, 2010 at 13:11
  • @Thierry I think this is not the most efficient way to load entire intepreter Commented Jun 4, 2010 at 13:16
  • don't use the accepted answer method. Look at the next one (in term of vote) i think it summarizes well your possibilities, but yes it do not answer directly your question ;-) Commented Jun 4, 2010 at 13:18
  • Can your string contain parentheses, and other operators such as ^ or ! ? Commented Jun 4, 2010 at 14:06

4 Answers 4

7
String value = ("false || true && true && false || true");
boolean result = false;
for (String conj : value.split("\\|\\|")) {
    boolean b = true;
    for (String litteral : conj.split("&&"))
        b &= Boolean.parseBoolean(litteral.trim());
    result |= b;
}
System.out.println(result); // prints true
Sign up to request clarification or add additional context in comments.

9 Comments

thank you @aioobe, this is good as no 3rd party lib is needed.
you could optimize the outer loop by breaking out of it (using a while instead of a for, or using a break at the end of the for) as soon as result is true: there is no way it can become false again
@Thierry, Sure. Doing a split using a regular expression is not the most efficient solution either :P
@aioobe that does |= b mean ?
@c0mrade result = result | b... similar to i += 5 but with or instead of plus.
|
1

If the only operators are && and ||, then I think this will work:

  static boolean eval(String str) {
    String s = str.replaceAll("\\s|\\|\\|false|false\\|\\|", "");
    return !s.contains("false") || s.contains("||true");
  }

For more complicated expressions, I found this library just for that. Don't know how efficient it is though.

Comments

0

You'll need a small boolean expressions grammar. A bit of recursive parsing should do the trick.

If you don't know how to write such a parser, you may use JavaCC or something similar.

Comments

0

there are parsergenerators available for which you can define a grammar.

But if you only got || and && as operators and true and false as values you can easily do this by yourself, by implmenting a very simple finite state machine:

1.) Split the string into the tokens

2.) parse the left most value by using Boolean.parseBoolean(token) and safe it's value in some instance variable (your state)

3.) combine your instance variable with the next boolean token using the given operator

4.) Repeat step3 until you finished through the whole string

This seems to work although i havent thorougly tested it :)

public class BooleanFSParser {

    private boolean parse(String data) {
        String[] tokens=data.split("\\s");
        boolean state=Boolean.parseBoolean(tokens[0]);
        for (int i=1;i<(tokens.length / 2) + 1;i=i+2){
            if (tokens[i].equals("&&")){
                state=state && Boolean.parseBoolean(tokens[i+1]);
            }else{
                state=state || Boolean.parseBoolean(tokens[i+1]);
            }
        }
        return state;
    }

    public static void main(String[] args) {
        BooleanFSParser parser = new BooleanFSParser();
        boolean val = parser.parse("true && true || false");
        System.out.println(String.valueOf(val));
    }
}

thats should give you a cirrectly parsed value, but it will get a bit more complex if you allow brackets for example ;)

have fun and check here for the theory Finite-state_machine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.