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

Oussama

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

    }
}
Reilas

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.

edited at
0

Comments

0 comments
Login to comment

Related

I keep getting a "java.lang.StringIndexOutOfBoundsException" even though my for loop counter adjusts for length by subtracting 1

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1960)

String Index out of range: 1

Getting string index out of range? python 3

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7

Hibernate OGM - java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Getting IndexError: string index out of range in python

Range object throwing java.lang.StringIndexOutOfBoundsException

How to fix "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5" problem in Java

"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range"

Rebalance topology throws java.lang.StringIndexOutOfBoundsException: String index out of range: -1

java.lang.StringIndexOutOfBoundsException: String index out of range: 12 in java

StringIndexOutOfBoundsException: String index out of range: 0 while reading a file

"main" java.lang.StringIndexOutOfBoundsException: String index out of range: 17

Exception in thread "main java.lang.StringIndexOutOfBoundsException: String index out of range

Runtime Error for an anagram - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 8

java.lang.StringIndexOutOfBoundsException: String index out of range: 4

Getting java.lang.StringIndexOutOfBoundsException

Getting an IndexError: string index out of range

java.lang.StringIndexOutOfBoundsException: String index out of range: (again)

Java - java.lang.StringIndexOutOfBoundsException: String index out of range: 11

java.lang.StringIndexOutOfBoundsException: String index out of range: -23 error

I keep getting the error "string index out of range"

Getting String index out of range with for loop in Java

I am unable to solve this :java.lang.StringIndexOutOfBoundsException: String index out of range: 54

Java String Concatenation Error "String index out of range: -1"

using recursion error. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

Getting an error "string index out of range" in python