Posted In: Java Core, String

How to parse a mathematical expression given as a string – using Bean Shell

Evaluating a math expression given in string form

Maven beanshell

<dependency>
    <groupId>org.beanshell</groupId>
    <artifactId>bsh</artifactId>
    <version>2.1b5</version>
</dependency>

<repository>
	<id>boundless</id>
	<url>http://repo.boundlessgeo.com/main/</url>
</repository>

beanshell import


package com.example.mathexpression;

import org.junit.Test;

import bsh.Interpreter;

public class MathExpression {

	@Test
	public void evaluate1() throws Exception {
		System.out.println("\nevaluate1() start");
		Interpreter interpreter = new Interpreter();
		interpreter.eval("result = 5+4*(7-15)");
		System.out.println(interpreter.get("result"));
	}
}

Tags:

by , on August 30th, 2017

  • Categories