How to Evaluate Math Expression Given in String Form in Java?
Last Updated :
28 Apr, 2025
Evaluating a mathematical expression given in string form is a common task in Java programming, often encountered in applications involving mathematical computations or user input processing.
- One approach involves using the ScriptEngine class from the javax.script package, which allows for the dynamic execution of scripts, including mathematical expressions written in languages like JavaScript.
- Another approach involves the Stack data structure using which we can evaluate string mathematical expressions.
Example of Evaluate String Expressions:
Example 1: Input String: "10-4*5"
Output: -10
Example 2: Input String: "3.5 + 2.8 * ( 4 - 1.2 ) / 2"
Output: 7.42
Program to Evaluate Math Expression Given in String Form in Java
There are certain methods to convert and evaluate a math expression given in string form in Java as mentioned below:
- Using ScriptEngineManager Class
- Using Stack or logical method
1. Using ScriptEngineManager Class
The Nashorn JavaScript script engine, its APIs, and its tool are deprecated in Java 11. Future iterations of Java will eliminate them. JDK 8 was the most current version to incorporate the Nashorn JavaScript engine.
Java
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws ScriptException
{
// Create a ScriptEngineManager to manage script engines
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine
= manager.getEngineByName("JavaScript"); // Retrieve a JavaScript engine from the manager
String str = "10-4*5"; // Define a mathematical expression to evaluate
System.out.println(engine.eval(str));
}
}
Output:
Below is the output of the program in console:

2. Using Stack or logical method
Java
import java.util.*;
import java.util.Stack;
public class Main {
// Function to evaluate a mathematical expression given
// in string form
public static double
evaluateExpression(String expression)
{
char[] tokens = expression.toCharArray();
// Stacks to store operands and operators
Stack<Double> values = new Stack<>();
Stack<Character> operators = new Stack<>();
// Iterate through each character in the expression
for (int i = 0; i < tokens.length; i++) {
if (tokens[i] == ' ')
continue;
// If the character is a digit or a decimal
// point, parse the number
if ((tokens[i] >= '0' && tokens[i] <= '9')
|| tokens[i] == '.') {
StringBuilder sb = new StringBuilder();
// Continue collecting digits and the
// decimal point to form a number
while (i < tokens.length
&& (Character.isDigit(tokens[i])
|| tokens[i] == '.')) {
sb.append(tokens[i]);
i++;
}
// Parse the collected number and push it to
// the values stack
values.push(
Double.parseDouble(sb.toString()));
i--; // Decrement i to account for the extra
// increment in the loop
}
else if (tokens[i] == '(') {
// If the character is '(', push it to the
// operators stack
operators.push(tokens[i]);
}
else if (tokens[i] == ')') {
// If the character is ')', pop and apply
// operators until '(' is encountered
while (operators.peek() != '(') {
values.push(applyOperator(
operators.pop(), values.pop(),
values.pop()));
}
operators.pop(); // Pop the '('
}
else if (tokens[i] == '+' || tokens[i] == '-'
|| tokens[i] == '*'
|| tokens[i] == '/') {
// If the character is an operator, pop and
// apply operators with higher precedence
while (!operators.isEmpty()
&& hasPrecedence(tokens[i],
operators.peek())) {
values.push(applyOperator(
operators.pop(), values.pop(),
values.pop()));
}
// Push the current operator to the
// operators stack
operators.push(tokens[i]);
}
}
// Process any remaining operators in the stack
while (!operators.isEmpty()) {
values.push(applyOperator(operators.pop(),
values.pop(),
values.pop()));
}
// The result is the only remaining element in the
// values stack
return values.pop();
}
// Function to check if operator1 has higher precedence
// than operator2
private static boolean hasPrecedence(char operator1,
char operator2)
{
if (operator2 == '(' || operator2 == ')')
return false;
return (operator1 != '*' && operator1 != '/')
|| (operator2 != '+' && operator2 != '-');
}
// Function to apply the operator to two operands
private static double applyOperator(char operator,
double b, double a)
{
switch (operator) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new ArithmeticException(
"Cannot divide by zero");
return a / b;
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
String exp = "3.5 + 2.8 * ( 4 - 1.2 ) / 2";
double result = evaluateExpression(exp);
System.out.println("Result: " + result);
}
}
Illustration:
- Java provides multiple methods for evaluating mathematical expressions given in string form.
- The straightforward approach involves utilizing the ScriptEngine class from the javax.script package, leveraging its capability to dynamically evaluate expressions, particularly those in JavaScript.
- Otherwise we can implement it using stack data structure.
Similar Reads
Extract all integers from the given String in Java In Java, we can extract integers from a given string using several approaches such as regular expressions, character iteration, or the split() method. In this article, we will learn how to extract all integers from the given String in Java.Example:We will use regular expressions to extract all integ
3 min read
How to Convert a String Class to an Integer Class in Java? String in Java is used to store the sequence of characters in Java. A string can contain a character, a word, a sentence, or a paragraph. Integer is a Wrapper class that is used to store Integer values in Java. The range of the Integer class is from -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1
3 min read
Finding Data Type of User Input using Regular Expression in Java Given a string, the task is to find its corresponding datatype using regular expression in java. We can broadly classify all data types into following types: Integer: Numeric datatypes like byte, short, int, long take the form of an Integer object.Double: Decimal datatypes like float and double take
2 min read
How to Convert a String to an Numeric in Java? In Java programming, there are situations in which we must convert a string of numeric characters into one of the following data types: double, float, long, or int. To execute arithmetic operations and other numerical calculations, this conversion is necessary. We will look at how to convert a strin
2 min read
How to Convert a String to an Long in Java ? In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210
2 min read