From 3297fd0c471542cec3016df31236d078006f20ec Mon Sep 17 00:00:00 2001 From: sivan Date: Mon, 7 Oct 2019 18:47:01 +0530 Subject: [PATCH] Fixed issue #6 - Crash on pressing operator buttons. --- .../java/com/exuberant/calci/HomeActivity.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/exuberant/calci/HomeActivity.java b/app/src/main/java/com/exuberant/calci/HomeActivity.java index 82471e3..55ae2d8 100644 --- a/app/src/main/java/com/exuberant/calci/HomeActivity.java +++ b/app/src/main/java/com/exuberant/calci/HomeActivity.java @@ -3,6 +3,7 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; @@ -11,7 +12,9 @@ public class HomeActivity extends AppCompatActivity implements View.OnClickListener { + private static final String TAG = "HomeActivity"; private String currentNumber = "", totalCalculation = ""; + private TextView totalCalculationTextView, currentAnswerTextView; @Override @@ -66,6 +69,7 @@ public void onClick(View view) { calculateAnswer(); break; + //Handle other numerical button clicks default: currentNumber += button.getText().toString(); @@ -83,8 +87,12 @@ private void handleOperatorClick(String operator){ totalCalculation += currentNumber + operator; currentNumber = ""; } else { - totalCalculation = totalCalculation.substring(0, totalCalculation.length() - 1); - totalCalculation += operator; + if (!totalCalculation.isEmpty()) { + + totalCalculation = totalCalculation.substring(0, totalCalculation.length() - 1); + totalCalculation += operator; + + } } } @@ -107,6 +115,8 @@ private double div(double a, double b){ private void calculateAnswer(){ //Use totalCalculation string to get final answer and display it double answer = 0.0; + currentNumber = ""; + Log.d(TAG, "calculateAnswer: totalCalculationString : " + totalCalculation); updateDisplay(); } }