Click here to Skip to main content
15,883,770 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a calculator app in android using java,I dont want to use tons of if/else statements to calculate the expressions. I was wondering if we have something like Eval() function ,just as we have in Javascript to evaluate a expression.

I have a GetResult() method where I would like tocalculate the numeric expression or evaluate the numeric expression.


Calculator Activity code :



package com.deepesh.calculatorx;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Cal_page extends AppCompatActivity {

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


        displayer=findViewById(R.id.displayer);
       
    }

    public void Addone(View view) {

        displayer.append("1");
    }

    public void Addtwo(View view) {
        displayer.append("2");
    }

    public void Addthree(View view) {
        displayer.append("3");
    }

    public void Addfour(View view) {
        displayer.append("4");
    }

    public void Addfive(View view) {
        displayer.append("5");
    }

    public void Addsix(View view) {
        displayer.append("6");
    }

    public void Addseven(View view) {
        displayer.append("7");
    }

    public void Addeight(View view) {
        displayer.append("8");
    }

    public void Addnine(View view) {
        displayer.append("9");
    }

    public void Addzero(View view) {
        displayer.append("0");
    }


    public void Addplus(View view) {
        displayer.append("+");
    }

    public void Addsubtract(View view) {
        displayer.append("-");
    }

    public void Addmultiply(View view) {
        displayer.append("*");
    }

    public void AddDivide(View view) {
        displayer.append("/");
    }

    public void AddDot(View view) {
        displayer.append(".");
    }

    public void clearALL(View view){
        displayer.setText(" ");
    }

    public void GetResult(View view) {
   //user presses Equal sign
        //evauluate the numeric expression and get result

    }

    public void clearLastchar(View view) {

        CharSequence numbers = displayer.getText();
        String Snumbers = numbers.toString();

        String Nnumbers = Snumbers.substring(0,Snumbers.length()-1);

        displayer.setText(Nnumbers);

    }
}


What I have tried:

I have tried using certain external Parsers, but most of them are outdated and get bugs
,and dont work.

Please suggest something latest and only something you have used.
Posted
Updated 29-Jul-20 20:51pm

1 solution

So basically, you have to write a calculator, and you don't want to do the "difficult bit" that you might learn something useful from?

You could try this: java - How to evaluate a math expression given in string form? - Stack Overflow[^]

But ... be aware that not only will you learn nothing useful from this assignment, but you will almost certainly get a poor grade as well: teachers can tell when you copy'n'paste a solution instead of "rolling your own" ...

I'd strongly suggest that you look at writing a parser to check the expression syntax and build an execution tree or RPN stack while it's doing it for evaluation when it's shown to be valid. Then execute the tree / stack to produce your result. You will learn a whole load more about how to do things, and if your aim is to become and advanced Java programmer as you said two weeks ago then you need to actually practice writing code: Can you please list me all the necessary things I should learn in android , to become an advanced or atleast good with building android apps ?[^]
Juist grabbing somebody else's code, finding it doesn't work in your app for some reason, and abandoning it isn't a good way to get anywhere towards that goal.
 
Share this answer
 
Comments
David Crow 30-Jul-20 8:23am    
"I'd strongly suggest that you look at writing a parser to check the expression syntax and build an execution tree or RPN stack while it's doing it for evaluation when it's shown to be valid. Then execute the tree / stack to produce your result."

One of my more favorite assignments when I was in school.
[no name] 30-Jul-20 22:41pm    
Hey ! Thats a great advice.Thanks man.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900