Executing math equation in Android

I need to be able to handle a math equation such as "(45+9)/8" in my app. I wanted to just eval it using JavaScript, but realized that I can't use javax.script in Android. So, I found WebView, but I'm having a bit of trouble using it. Most of the examples refer to using an external page with the JS code, or using "javascript: var return . etc." I'd need to use the latter, but I've been having a bit of trouble with returning the variable to my app. Is it possible to have the JS eval it and then write the value to a hidden TextView?

asked Apr 5, 2012 at 4:28 2,468 2 2 gold badges 26 26 silver badges 41 41 bronze badges Dude, no need for javascript. All you need is the standard Java to calculate math equations. Commented Apr 5, 2012 at 4:59 I'm using it for user entered equations. Meaning that I have to be able to eval a string. Commented Apr 5, 2012 at 22:03

4 Answers 4

Check out exp4j. It's a simple expression evaluator for java. For the equation you posted in your question you could just do:

Calculable calc = new ExpressionBuilder("(45+9)/8").build() double result1=calc.calculate(); 
answered Apr 5, 2012 at 5:09 Kurtis Nusbaum Kurtis Nusbaum 30.8k 13 13 gold badges 80 80 silver badges 105 105 bronze badges

Your answer definitely looks like it'll be a better solution than I had hoped for. Where do I put the de and META-INF folders in the android app?

Commented Apr 5, 2012 at 22:40 All you have to do is just put the jar in your /libs directory. Commented Apr 6, 2012 at 16:29
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.swing.JOptionPane; private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) < String expression = txtResult.getText(); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); try < result = engine.eval(expression).toString(); txtResult.setText(result); >catch (Exception e) < JOptionPane.showMessageDialog(null, txtResult.getText() + " cannot be calculated. Try again!", "Error on calculation!", JOptionPane.WARNING_MESSAGE); txtResult.setText(""); >> 
1,411 6 6 gold badges 26 26 silver badges 33 33 bronze badges answered Jan 29, 2016 at 12:15 user5505982 user5505982 I can't import the needed classes in Android. Commented Apr 8, 2019 at 16:34

Interesting option for more advanced expression could be to turn some online calculator into a webservice that you can use from your mobile.

answered Apr 5, 2012 at 6:19 38.1k 15 15 gold badges 117 117 silver badges 175 175 bronze badges

What about when there is no connection available? If you can evaluate the expression without reachability is better then forcing to that.

Commented Apr 8, 2019 at 16:33

You can do this using exp4j in android studio.

dependencies

Now you can try the following demo,

package com.example.expressionevaluator; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import de.congrace.exp4j.Calculable; import de.congrace.exp4j.ExpressionBuilder; import de.congrace.exp4j.UnknownFunctionException; import de.congrace.exp4j.UnparsableExpressionException; public class MainActivity extends AppCompatActivity < @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Calculable calc = null; try < calc = new ExpressionBuilder("(200 + 100) / 2 + 300").build(); double result = calc.calculate(); Log.d("result", result); >catch (UnknownFunctionException e) < e.printStackTrace(); >catch (UnparsableExpressionException e) < e.printStackTrace(); >> >