Skip to content
Advertisement

Java Calculator App On Android Studio isn’t functioning how it is supposed to

I am a beginner, so please don’t judge me if this is a silly mistake or something. I am trying to make a calculator using Java and android studio, but there seems to be a problem when adding, subtracting and multiplying. On debugging, I found that the variable ans is coming to be 0 automatically when adding. What is the problem here. Please help.

package com.example.simplecalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView tvHead;
EditText etDisplay;
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnPlus, btnMinus, btnDiv, btnMul, btnDot, btnEquals, btnClear;
float res1, res2;
float ans;
boolean add, subtract, divide, multiply;

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

    tvHead = findViewById(R.id.tvHead);
    etDisplay = findViewById(R.id.etDisplay);
    btn0 = findViewById(R.id.btn0);
    btn1 = findViewById(R.id.btn1);
    btn2 = findViewById(R.id.btn2);
    btn3 = findViewById(R.id.btn3);
    btn4 = findViewById(R.id.btn4);
    btn5 = findViewById(R.id.btn5);
    btn6 = findViewById(R.id.btn6);
    btn7 = findViewById(R.id.btn7);
    btn8 = findViewById(R.id.btn8);
    btn9 = findViewById(R.id.btn9);
    btnPlus = findViewById(R.id.btnPlus);
    btnMinus = findViewById(R.id.btnMinus);
    btnMul = findViewById(R.id.btnMul);
    btnDiv = findViewById(R.id.btnDiv);
    btnDot = findViewById(R.id.btnDot);
    btnEquals = findViewById(R.id.btnEquals);
    btnClear = findViewById(R.id.btnClear);

    btn0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText()   + "0");

        }
    });
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText()   + "1");

        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() +"2");

        }
    });
    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() + "3");

        }
    });
    btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() + "4");

        }
    });
    btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() + "5");

        }
    });
    btn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() + "6");

        }
    });
    btn7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() + "7");

        }
    });
    btn8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() + "8");

        }
    });
    btn9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() + "9");

        }
    });
    btnDot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(etDisplay.getText() + ".");

        }
    });
    btnClear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            etDisplay.setText(null);
        }
    });

    btnPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            res1 = Float.parseFloat(etDisplay.getText().toString());
            etDisplay.setText(null);
            add = true;

        }
    });

    btnMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            res1 = Float.parseFloat(etDisplay.getText().toString());
            etDisplay.setText(null);
            subtract = true;

        }
    });

    btnMul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            res1 = Float.parseFloat(etDisplay.getText().toString());
            etDisplay.setText(null);
            multiply = true;

        }
    });

    btnDiv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            res1 = Float.parseFloat(etDisplay.getText().toString());
            etDisplay.setText(null);
            divide = true;

        }
    });

   btnEquals.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {

           res2 = Float.parseFloat(etDisplay.getText().toString());

           if (etDisplay.getText().toString().isEmpty())
           {
               etDisplay.setText("");
           }

           if (add = true)
           {
               ans = res1 + res2;
               add = false;
           }
           if (subtract = true)
           {
               ans = res1-res2;
               subtract = false;
           }
           if (multiply = true)
           {
               ans = res1*res2;
               multiply = false;
           }
           if (divide = true)
           {
               ans = res1/res2;
               divide = false;
           }

           String answer = ans+"";
           etDisplay.setText(answer);

       }
   });




}

}

Advertisement

Answer

You are accidentally assigning a value to your booleans, rather than checking if they are true:

if (add = true)
// ...

if (subtract = true)
// ...

if (multiply = true)
// ...

if (divide = true)
// ...

You should use == to compare, not =. However, for boolean, you don’t need to compare at all:

if (add)
// ...

if (subtract)
// ...

if (multiply)
// ...

if (divide)
// ...
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement