- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to parse a mathematical expression and operators
At first, we have set the mathematical expressions:
String one = "10+15*20-5/5"; String two = "3+5-6"; String three = "9+2*(6-3+7)";
To parse mathematical expression, use Nashorn JavaScript in Java i.e. scripting. Nashorn invoke dynamics feature, introduced in Java 7 to improve performance.
For scripting, use the ScriptEngineManager class for the engine:
ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("Nashorn");
Now for JavaScript code from string, use eval i.e. execute the script. Here, we are parsing mathematical expressions set above:
Object expResult1 = scriptEngine.eval(one); Object expResult2 = scriptEngine.eval(two); Object expResult3 = scriptEngine.eval(three);
Example
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Demo { public static void main(String[] args) throws Exception { String one = "10+15*20-5/5"; String two = "3+5-6"; String three = "9+2*(6-3+7)"; String four = "(10 % 3)*2+6"; String five = "3*3+3"; ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("Nashorn"); Object expResult1 = scriptEngine.eval(one); Object expResult2 = scriptEngine.eval(two); Object expResult3 = scriptEngine.eval(three); Object expResult4 = scriptEngine.eval(four); Object expResult5 = scriptEngine.eval(five); System.out.println("Result of expression1 = " + expResult1); System.out.println("Result of expression2 = " + expResult2); System.out.println("Result of expression3 = " + expResult3); System.out.println("Result of expression4 = " + expResult4); System.out.println("Result of expression5 = " + expResult5); } }
Output
Result of expression1 = 309 Result of expression2 = 2 Result of expression3 = 29 Result of expression4 = 8 Result of expression5 = 12
- Related Articles
- HTML5 Mathematical operators
- Java Program to parse date and time
- Java Program to Parse and Format a Number into Binary
- What are basic JavaScript mathematical operators?
- Java Program to convert mathematical string to int
- Program to evaluate one mathematical expression without built-in functions in python
- Java Program to evaluate mathematical expressions in String
- Evaluating a string as a mathematical expression in JavaScript
- Java Program to parse Time using custom format
- Evaluating a mathematical expression considering Operator Precedence in JavaScript
- Parse and format a number to octal in Java
- Parse and format a number to decimal in Java
- Parse and format to hexadecimal in Java
- Java Program to parse string date value with default format
- Expression Add Operators in C++

Advertisements