1

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());
        });
    }
}
TofferJ
  • 4,678
  • 1
  • 37
  • 49
  • Do you know what `Integer.parseInt` does if the string you pass it is not a valid integer? – tgdavies Jan 30 '23 at 20:28
  • It would probably help you if you wrote out in English (or your first language, as this is an exercise for yourself) exactly what your listener does. Then think, does it satisfy "before you do anything, ensure that the number boxes aren’t empty so an error doesn’t throw"? – tgdavies Jan 30 '23 at 20:30
  • 1
    And see https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513839#513839 – tgdavies Jan 30 '23 at 20:31

1 Answers1

1

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)

Thorne
  • 186
  • 1
  • 9