Using Javaluator : [Parsing and evaluating a expression]

CHECK OUT THE PROJECT IN GITHUB

Hello Guys ….

Welcome again to this new tutorial and in this tutorial i will show you how to parse and evaluate a string expression in android studio using Javaluator library. You can use this javaluator library to make a scientific calculator and evaluate strings in other apps too.


  • Fire up your Android Studio (I would recommend you to use Android Studio over Eclipse).
  • Create a new project and give your package name and project name, etc . (NOTE-¬†If you are copying the code from this site, make sure you change the package name com.gmonetix.stringevaluator to your_own_package_name )
  • Now¬†download Javaluator jar file from here .
  • Change to project structure.
  • Paste the downloaded jar file into libs folder.
  • Rigt click on the jar file and click Add as library and click OK on the App module and let the project sync.

 

 

 

 

 

  • Now go to activity_main.xml layout file and paste the following code for making the UI.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter expression"
        android:id="@+id/etExpression"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <TextView
        android:textSize="50sp"
        android:id="@+id/tvresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etExpression"
        android:hint="0" />
    <Button
        android:text="See Result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvresult"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/seeResult" />
</RelativeLayout>
  • Now open MainActivity class and paste the following code.
package com.gmonetix.stringevaluator;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fathzer.soft.javaluator.DoubleEvaluator;

public class MainActivity extends Activity {

    TextView tvResult;
    EditText etExpression;
    Button btn;
    Editable expression;
    Double result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etExpression = (EditText) findViewById(R.id.etExpression);
        tvResult = (TextView) findViewById(R.id.tvresult);
        btn = (Button) findViewById(R.id.seeResult);
        expression = etExpression.getText();
        
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    // Create an object of DoubleEvaluator for getting result in double type
                    DoubleEvaluator evaluator = new DoubleEvaluator();
                    result = evaluator.evaluate(String.valueOf(expression));
                    tvResult.setText(result.toString());
                }
                // Catching error for wrong expressions otherwise app will crash
                catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
}
  • Now run and test the app. Enter expressions in the edit text box like (45+53*4314/4315) , (sin(432)*43434*tan(42)),etc.
  • Enter valid expressions with proper bracket endings.

FOR MORE TUTORIAL ON JAVALUATOR CLICK HERE

If you get any error be sure to comment down below. Get ready for the next tutorials !!!!

Leave a Reply

Your email address will not be published. Required fields are marked *