Android calculator getting crashes

Issue

I’m making a simple android calculator that has to support +,-,*,/ .

When I try to press any button – the app crashes. After some investigations, I found out that the problem is in the inner if statement :

public void buttonFunctionNumber(View view) {
        if (view instanceof Button){
            Button button = (Button) view;
            String str = button.getText().toString();
            
            if (Double.parseDouble(result.getText().toString()) == 0)
                result.setText(str);
            else
                result.append(str);
        }
    }

The if: after the user pressed the desired number, and right before he prasses the second – the calculator’s screen will show a 0. So in this if I want to check whether the result is 0 (the result is what the calculator’s screen shows) to know that the user want to continue to the next number.

the else: the user wants to continue adding more digits to the number for the calculations so we append the next digit.

I would like to know how can I solve this problem.

Thanks for your time 🙂

The whole code:

package com.example.myfirstproj;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView result;
    double num1=0.0, num2=0.0;
    String sign = "";


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

        result = findViewById(R.id.textViewResult);
    }


    public void buttonFunctionNumber(View view) {
        if (view instanceof Button){
            Button button = (Button) view;
            String str = button.getText().toString();

            if (Double.parseDouble(result.getText().toString()) == 0.0)
                result.setText(str);
            else
                result.append(str);
        }
    }

    public void buttonEqualFunction(View view) {
        if (view instanceof Button) {
            String temp2 = result.getText().toString();
            num2 = Double.parseDouble(temp2);
            double res = 0.0;

            switch (sign) {
                case "+":
                    res = num1 + num2;
                    break;
                case "-":
                    res = num1 - num2;
                    break;
                case "*":
                    res = num1 * num2;
                    break;
                case "/":
                    if (num2 != 0.0)
                        res = num1 / num2;
                    else
                        Toast.makeText(this, "Error Cant divide by 0", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;

            }
            num1 = res;
            result.setText(res + "");
        }
    }

    public void buttonAddFunction(View view) {
        if (view instanceof Button){
            String temp1 = result.getText().toString();
            num1 = Double.parseDouble(temp1);
            sign = "+";
            result.setText(" ");
        }
    }

    public void buttonSubtractFunction(View view) {
        if (view instanceof Button) {
            String temp1 = result.getText().toString();
            num1 = Double.parseDouble(temp1);
            sign = "-";
            result.setText("0");
        }
    }

    public void buttonMultiplyFunction(View view) {
        if (view instanceof Button) {
            String temp1 = result.getText().toString();
            num1 = Double.parseDouble(temp1);
            sign = "*";
            result.setText("0");
        }
    }

    public void buttonDivideFunction(View view) {
        if (view instanceof Button) {
            String temp1 = result.getText().toString();
            num1 = Double.parseDouble(temp1);
            sign = "/";
            result.setText("0");
        }

    }

    public void buttonClearFunction(View view) {
        result.setText("0");
    }
}

Solution

Try trimming your result and checking for null and empty first before parsing

String result = result.getText().toString()).trim();
if (result != null && result.length() > 0) {
   if (Double.parseDouble(result) == 0.0)
      result.setText(str);
   else
      result.append(str);
}

Answered By – Lawrence Gimenez

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published