1

Say a string contains the expression 1<3... how can I evaluate that expression in an if statement?

import java.io.*;

public class EvaluateExpression
{
   public static void main(String[]args)throws IOException
   {
     InputStreamReader isr = new InputStreamReader(System.in);
     BufferedReader in = new BufferedReader(isr);

      String expression;

      System.out.print("Enter the expression to evaluate: ");
      expression = in.readLine();

      if (expression){
      System.out.print("The expression " + expression + " is true.");
      }else{
      System.out.print("The expression " + expression + " is not true.");
      }           
   }
}

A boolean is required but a String is found. Is there a way to evaluate the string so it returns a boolean? if (1<3) {...} works but if (expression) {...} where the String variable expression is 1<3 doesn't work. Casting it to boolean doesn't work either.

Thanks!

Baldwin D.
  • 110
  • 1
  • 6

4 Answers4

0

You can use ScriptEngine coming with the JDK1.6

For Example :

 ScriptEngineManager sem = new ScriptEngineManager();
 ScriptEngine se = sem.getEngineByName("JavaScript");

 InputStreamReader isr = new InputStreamReader(System.in);
 BufferedReader in = new BufferedReader(isr);
 String expression;

  System.out.print("Enter the expression to evaluate: ");
  expression = in.readLine();
  String result = se.eval(expression).toString();
  if (result.equals("true")) {
      System.out.print("The expression " + expression + " is true.");
  } else {
       System.out.print("The expression " + expression + " is not true.");
  }
Alya'a Gamal
  • 5,624
  • 19
  • 34
0

You can split the string using >, <, >=, <=, ==, != as delimeters. Check the 2 strings in the returned array from the split function, and convert them to int.

then use something like this in a function

if (youroperator.equals("<"))
    return first_number < second_number
elseif (youroperator.equals(">"))
    return first_number>second_number
...

your operator can be obtained from the string.contains() function

Kevin Joymungol
  • 1,764
  • 4
  • 20
  • 30
0

If you want a generic solution you may use the script engine built into the JVM.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
...
...
String expression = "1<3";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jsEngine = manager.getEngineByName("JavaScript"); //BE CAREFUL about the engine name. 
Object result = jsEngine.eval(expression); //Returns a java.lang.Boolean for the expression "1<3"

Note: Though "JavaScript" engine name works in my environment, in your environment it may not be available OR it may be called by other names like "js" or "javascript". In some environments other engines like "Groovy" engine may be available. Use any engine available in your environment.

RaviH
  • 3,544
  • 2
  • 15
  • 14
0

try...

if(expression.equals("1<3"))
{}
else
{}

"if" takes boolean as parameter ,you cannot pass a string in it.So there is equals method java.lang.String which check whether two strings are equal or not and its return type is boolean(true or false).

Devavrata
  • 1,785
  • 17
  • 30