0
    String input = sc.nextLine();
    System.out.println(Boolean.parseBoolean(input));

So my inputs look like 1 > 2 or false || true, but I'm not getting the correct values.

But when I call:

    Boolean x = 1 < 2;
    System.out.println(x);

I get them correct. So my question is. How do I convert String input to return boolean value?

So far tried:

    Boolean.parseBoolean();
    Boolean.getBoolean();
    Boolean.valueOf();

Thanks for your answers.

EDIT

INPUT         -  OUTPUT
1 < 2         -  false(should be true)
false || true -  false(should be true)
Seven
  • 55
  • 8

4 Answers4

4

You can use ScriptEngine for this:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
String input = "1 > 2";
System.out.println(engine.eval(input)); //prints false
thegauravmahawar
  • 2,621
  • 3
  • 12
  • 22
  • I actually had this done in js, but I had no idea there was such thing as ScriptEngine, thanks a ton! – Seven Nov 07 '15 at 18:51
1

Boolean.parseBoolean(String) is implemented as:

public static boolean parseBoolean(String s) {
    return ((s != null) && s.equalsIgnoreCase("true"));
}

So it returns true if the input string is not null and equals true (ignoring case). Else false is returned.

You seem to expect that Boolean.parseBoolean(String) accepts an expression string and evaluates the string, but that is simply not the case.

wero
  • 30,527
  • 3
  • 46
  • 72
1

While you write System.out.println(1<2); it works because there is a overloaded version of println() which takes boolean value. But when you pass a string, it will be considered as String only, NOT boolean.

In addition, Boolean.parseBoolean() always takes boolean value(true\false) in string format as parameter and returns corresponding value in boolean format, otherwise it will always return false

Rehman
  • 3,465
  • 3
  • 17
  • 31
0

Boolean.parseBoolean does the following, according to the docs:

public static boolean parseBoolean(String s)

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

You're attempting to pass in an expression that will evaluate to true or false, rather than the string "true" or "True" or "TRUE". Therefore, this will work like so:

Boolean.parseBoolean("true"); // equals true
Boolean.parseBoolean("1 > 2"); // not the string "true", therefore returns false.

Generally speaking, you should be very cautious about dynamic execution of code contained in strings. If the string is being passed in from a user, it's quite dangerous. My advice would be to find a better way.

If you really need to do this:

  • One of the other answers mention using ScriptEngine. Please be aware that this evaluates the string as JavaScript, not as Java. For simple boolean expressions, that's probably fine, but you should be aware of it. JavaScript has rules for truthiness that are perplexing at times, and they will not evaluate the same in Java.

For example, try running this in ScriptEngine:

1 > '0' // true in JavaScript, but probably unexpected in your code!
  • If you want to do this all properly in Java, you could use the Java Compiler API. Unfortunately, it's not as simple as simply feeding expressions as strings to the ScriptEngine. This post might be useful, however, for in-memory compilation of Java code.
Community
  • 1
  • 1