最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Executing math equation in Android - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Apr 5, 2012 at 4:28 RhyonoRhyono 2,4682 gold badges27 silver badges42 bronze badges 2
  • Dude, no need for javascript. All you need is the standard Java to calculate math equations. – Eng.Fouad 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. – Rhyono Commented Apr 5, 2012 at 22:03
Add a ment  | 

4 Answers 4

Reset to default 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();

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

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

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 .example.expressionevaluator;

import androidx.apppat.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/exp4j/

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

发布评论

评论列表(0)

  1. 暂无评论