Hello guys i'm new to android development and i'm currently trying to build a simple a calculator however when i click the c button it just turns the last character to a "C" Here's the line of code i used:
if (buttonText.equals("C")) {
dataToCalculate = dataToCalculate.substring(0,dataToCalculate.length()-1);
}
`
and Here's the full code:
package com.example.icalculate;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.android.material.button.MaterialButton;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView textResult, textSolution;
private MaterialButton btnC,btnOpenBracket,btnClosedBracket,btnDivide;
private MaterialButton btn7,btn8,btn9,btnMultiply;
private MaterialButton btn4,btn5,btn6,btnPlus;
private MaterialButton btn1,btn2,btn3,btnMinus;
private MaterialButton btnAC,btn0,btnDot,btnEquals;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textResult = (TextView) findViewById(R.id.txtResult);
textSolution = (TextView) findViewById(R.id.txtSolution);
assignId(btnC, R.id.button_c);
assignId(btnOpenBracket, R.id.button_open_bracket);
assignId(btnClosedBracket, R.id.button_closed_bracket);
assignId(btnDivide, R.id.button_divide);
assignId(btn7, R.id.button_7);
assignId(btn8, R.id.button_8);
assignId(btn9, R.id.button_9);
assignId(btnMultiply, R.id.button_multiply);
assignId(btn4, R.id.button_4);
assignId(btn5, R.id.button_5);
assignId(btn6, R.id.button_6);
assignId(btnPlus, R.id.button_plus);
assignId(btn1, R.id.button_1);
assignId(btn2, R.id.button_2);
assignId(btn3, R.id.button_3);
assignId(btnMinus, R.id.button_minus);
assignId(btnAC, R.id.button_ac);
assignId(btn0, R.id.button_0);
assignId(btnDot, R.id.button_dot);
assignId(btnEquals, R.id.button_equals);
}
void assignId(MaterialButton btn, int id) {
btn = findViewById(id);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
MaterialButton button = (MaterialButton) v;
String buttonText = button.getText().toString();
String dataToCalculate = textSolution.getText().toString();
if (buttonText.equals("AC")) {
textSolution.setText("");
textResult.setText("0");
return;
}
if (buttonText.equals("C")) {
dataToCalculate = dataToCalculate.substring(0,dataToCalculate.length()-1);
}
if (buttonText.equals("=")) {
textSolution.setText(textResult.getText());
}else {
dataToCalculate = dataToCalculate+buttonText;
}
textSolution.setText(dataToCalculate);
}
}
There is more than one situation here.
Firstly, if you're encountering a StringIndexOutOfBoundsException, then it would mean dataToCalculate has a length of 0.
Because, dataToCalculate.length() - 1, on a length of 0, will return −1.
You can remedy this by checking the length first—I typically use String#isEmpty.
if (!dataToCalculate.isEmpty()) {
dataToCalculate = dataToCalculate.substring(0,dataToCalculate.length()-1);
}
Furthermore, you can simplify your code to the following.
switch (buttonText) {
case "AC" -> {
textSolution.setText("");
textResult.setText("0");
return;
}
case "C" -> {
if (!dataToCalculate.isEmpty())
dataToCalculate = dataToCalculate.substring(0, dataToCalculate.length() - 1);
}
case "=" -> textSolution.setText(textResult.getText());
default -> dataToCalculate = dataToCalculate+buttonText;
}
textSolution.setText(dataToCalculate);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments