-4

Say I have a String

String strCondition = something.contains("value") && somethingElse.equals("one");

How do I convert this String into a boolean condition so that I can be able to use it in an IF statement?

If I use valueOf(), will it evaluate the contents of the String?

RE-EDIT: I am not sure how to put this.

I am taking the value something.contains("value") && somethingElse.equals("one") from a database column. If I try to assign that to boolean variable it shows a type mismatch.

Anjan Baradwaj
  • 1,219
  • 5
  • 27
  • 54
  • If you have multiple strings but a fixed set of them, then you can go for creating an enum out of them and then comparing them – AurA Jun 18 '14 at 06:27
  • 1
    Do you mean taking `strCondition` and turning it into code? – awksp Jun 18 '14 at 06:27
  • 1
    How do you even store the boolean value into a String in the first place? – Rahul Jun 18 '14 at 06:27
  • This won't even compile. Firstly, `&&` returns type `boolean`. Secondly, `String.contains()` is misspelled. – Unihedron Jun 18 '14 at 06:28
  • 1
    I think this could possibly be just a really badly communicated version of "how do I turn a string into code that I can use in a program later?" – awksp Jun 18 '14 at 06:29
  • "If I use valueOf(), will it evaluate the contents of the String?" Have you checked the Oracle docs? They might mention it. – Unihedron Jun 18 '14 at 06:33
  • OP: You might want to check [this](https://stackoverflow.com/questions/4389232/run-piece-of-code-contained-in-a-string) out. Reflection may or may not be an option, depending on if the structure of `strCondition` is predictable. – awksp Jun 18 '14 at 06:37
  • I don't think that even compiles `String strCondition = something.contains("value") && somethingElse.equals("one");`. As mentioned by others the expression is already a `boolean` – Eypros Jun 18 '14 at 07:04

5 Answers5

7

you don't.

it already is a boolean expression.

something.contains("value") -> this returns either true or false && somethingElse.equals("one"); -> this also returns true or false.

what you need, is either:

boolean strCondition = something.contains("value") && somethingElse.equals("one");
if ( strCondition )

or

if ( something.contains("value") && somethingElse.equals("one"))

EDIT:

The above would either return true, false, or throw a nasty NullPointerException.

To avoid the latter, you should use:

if ( "one".equals(somethingElse) && (something != null && something.contains("value"))
Stultuske
  • 9,296
  • 1
  • 25
  • 37
3

You can use these :

if (strCondition.equals("true")) 

if (Boolean.getBoolean(strCondition)) 

if (Boolean.valueOf(strCondition)) 
PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
2
something.contains("value") && somethingElse.equals("one");// returns a boolean . why put it into a String in the first place?
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

Read JavaDoc first!

Boolean.valueOf(String s) returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".

Sample code

if(Boolean.valueOf(strCondition)) {

}
MariuszS
  • 30,646
  • 12
  • 114
  • 155
1

contains and equals methods of string returns boolean you cannot cast to String

A Stranger
  • 551
  • 2
  • 11