7

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?

Rhyono
  • 2,420
  • 1
  • 25
  • 41

4 Answers4

11

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();
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
1

Try this one:

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("");
    }
}
Szabolcs Páll
  • 1,402
  • 6
  • 25
  • 31
0

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

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • What about when there is no connection available? If you can evaluate the expression without reachability is better then forcing to that. – IgniteCoders Apr 08 '19 at 16:33
0

You can do this using exp4j in android studio.

  • Download exp4j binary jar from the official site. Download

  • Import exp4j into android studio by copying the jar files in app/libs folder.

  • Add the following line in your module build.gradle file dependencies.

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        ...
    }
    

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();
        }
    }
}

For more:

https://lallafa.objecthunter.net/exp4j/

https://github.com/codemaker2015/Expression-Evaluator

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81