Home > Enterprise >  Simple calculator with java android / Don't crash
Simple calculator with java android / Don't crash

Time:01-31

This is a project in Codecademy.

I have a problem with the task : * Ensuring the app doesn’t crash when the equals button is pressed and one of the number boxes is empty.

When equals is pressed, before you do anything, ensure that the number boxes aren’t empty so an error doesn’t throw.

But with my code, the button equals is always false.

package com.codecademy.simplecalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText firstNumber;
    EditText secondNumber;
    RadioGroup operators;
    RadioButton add;
    RadioButton subtract;
    RadioButton divide;
    RadioButton multiply;
    Button equals;
    TextView result;

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

        firstNumber = findViewById(R.id.number1);
        secondNumber = findViewById(R.id.number2);
        operators = findViewById(R.id.Radio_Groupoperators);
        add = findViewById(R.id.add);
        subtract = findViewById(R.id.subtract);
        multiply = findViewById(R.id.multiply);
        divide = findViewById(R.id.divide);
        equals = findViewById(R.id.equals);
        result = findViewById(R.id.result);

        equals.setEnabled(false);

        /*firstNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                equals.setEnabled(true);
            }
        });*/

        equals.setOnClickListener(v -> {

            int firstNumberValue = Integer.parseInt(firstNumber.getText().toString());
            int secondNumberValue = Integer.parseInt(secondNumber.getText().toString());

            int operatorButtonId = operators.getCheckedRadioButtonId();

            Integer answer;

            if (operatorButtonId == add.getId()) {
                answer = firstNumberValue   secondNumberValue;
            } else if (operatorButtonId == subtract.getId()) {
                answer = firstNumberValue - secondNumberValue;
            } else if (operatorButtonId == multiply.getId()) {
                answer = firstNumberValue * secondNumberValue;
            } else {
                answer = firstNumberValue / secondNumberValue;
            }

            if (equals.getText().toString() == "") {
                Toast.makeText(this, "Tap a number", Toast.LENGTH_SHORT).show();
            } else {
                equals.setEnabled(true);
            }

            result.setText(answer.toString());
        });
    }
}

CodePudding user response:

Here's a simple answer, put these at the start of your onClickListener:

if (TextUtils.isEmpty(firstNumber.getText())) return;
if (TextUtils.isEmpty(secondNumber.getText())) return;

This will stop executing the function early if either firstNumber or secondNumber are empty strings.

Read more about this function here: https://developer.android.com/reference/android/text/TextUtils#isEmpty(java.lang.CharSequence)

  • Related