我遇到了java.lang.StringIndexOutOfBoundsException: 字符串索引越界: -1错误。

huangapple go评论71阅读模式
英文:

I'm getting java.lang.StringIndexOutOfBoundsException: String index out of range: -1

问题

如果点击"C"按钮,它只会将最后一个字符变成"C",这是我使用的代码行:

if (buttonText.equals("C")) {
    dataToCalculate = dataToCalculate.substring(0, dataToCalculate.length() - 1);
}

这是完整的代码:

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);

    }
}
英文:

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);
}
}

答案1

得分: 0

以下是您要翻译的内容:

有多种情况需要考虑。

首先,如果您遇到了一个 StringIndexOutOfBoundsException,那么这意味着 dataToCalculate 的长度为0。
因为在长度为0的情况下,dataToCalculate.length() - 1 会返回 -1。

您可以通过先检查长度来解决这个问题,通常我使用 String#isEmpty

if (!dataToCalculate.isEmpty()) {
    dataToCalculate = dataToCalculate.substring(0,dataToCalculate.length()-1);
}

此外,您可以简化您的代码如下。

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);
英文:

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);

huangapple
  • 本文由 发表于 2023年6月12日 05:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定