Java Program To Check Whether Expression Is Valid or Not? String Expression

  In this section, we will learn to create a java program to check whether the expression is valid or not in string format and return the boolean value. If the string is valid then return true otherwise false.

Java Parse Mathematical Expression In String

Let's create a basic example of a mathematical expression as shown below:

String validExpr = "(5+8)*((5-2)+(8-1))";
String invalidExpr = "(5+*8)*((5-2)+(8-1)";

To parse the mathematical expression in string format and check if it is valid or not, use JavaScript(js) engine in Java i.e. It is scripting.

To parse the mathematical expression in scripting, use the ScriptEngine class as shown below:

										
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
												
										
									

Create an object to store the results and use the eval function of javascript that return the evaluated value as mention below :

										
Object  result = engine.eval(expression);
												
										
									


Example 1: Java Program To check whether Expression is valid or not? Return True or False

										
/*;==========================================
; Title: Create a java program to check expression is valid or not using string.
; Author: codenaive littleboy8506@  <admin@codenaive.com> <s.8506.a@gmail.com>
; Date:   10 May 2021
;==========================================*/
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class CheckExpressionValid
{
    public static boolean checkExpression(String expression){
         ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
        try {
            Object  result = engine.eval(expression);
             return true;
        }
        catch (ScriptException e) {
            return false;

        }
       
    }
	public static void main(String[] args) {
	 

	    boolean result; 
	      
        String validExpr = "(5+8)*((5-2)+(8-1))";
        String invalidExpr = "(5+*8)*((5-2)+(8-1)";
        
        // evaluate valid expression  
        
        result = checkExpression(validExpr);
        if(result !=true){
            System.out.println(validExpr+" NOT a Valid Expression ");
        }
        else{
             System.out.println(validExpr+" is a Valid Expression ");
        }
        
        
        // evaluate invalid expression 
        result = checkExpression(invalidExpr);
        if(result !=true){
            System.out.println(invalidExpr+" NOT a Valid Expression ");
        }
        else{
             System.out.println(invalidExpr+" is a Valid Expression ");
        }
        
        
	}
}
	
												
										
									

Output 1:

										
(5+8)*((5-2)+(8-1)) is a Valid Expression 
(5+*8)*((5-2)+(8-1) NOT a Valid Expression 


												
										
									


Example 2: Java Program To check whether Expression is valid or not? Use Custom Input, Return True or False.

To evaluate with variables, use the following snippet code as shown below :

									
int x= 1;
String validExpr = "(5+"+x+")*((5-2)+(8-1))";
String invalidExpr = "(5+*8)*((5-"+x+")+(8-1)";									
									
								
										
/*;==========================================
; Title: Create a java program to check expression is valid or not using string (with input).
; Author: codenaive littleboy8506@  <admin@codenaive.com> <s.8506.a@gmail.com>
; Date:   10 May 2021
;==========================================*/
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class CheckExpressionValid
{
    public static boolean checkExpression(String expression){
         ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
        try {
            Object  result = engine.eval(expression);
             return true;
        }
        catch (ScriptException e) {
            return false;

        }
       
    }
	public static void main(String[] args) {
	 
	    
	    boolean result; 
	    int x= 1;
        String validExpr = "(5+"+x+")*((5-2)+(8-1))";
        String invalidExpr = "(5+*8)*((5-"+x+")+(8-1)";
        
        // evaluate valid expression  
        
        result = checkExpression(validExpr);
        if(result !=true){
            System.out.println(validExpr+" NOT a Valid Expression ");
        }
        else{
             System.out.println(validExpr+" is a Valid Expression ");
        }
        
        
        // evaluate invalid expression 
        result = checkExpression(invalidExpr);
        if(result !=true){
            System.out.println(invalidExpr+" NOT a Valid Expression ");
        }
        else{
             System.out.println(invalidExpr+" is a Valid Expression ");
        }
        
        
	}
}
	
												
										
									

Output 2:

										
(5+1)*((5-2)+(8-1)) is a Valid Expression 
(5+*8)*((5-1)+(8-1) NOT a Valid Expression 


												
										
									
Announcement

Advertisement