I have a problem with Java and Android Studio; the following code was to be a backspace button:
else if(view == btnBackspace){
int expressionLength = expression.length() - 2;
String expressionNew = newExpression.subSequence(0, expressionLength); // new expression is the t
editText.setText(expressionNew); // prints out text
}
I'm trying do a backspace button, I don't know if this is the better way of make this. So, subSequence method return's me that is a char sequence then I put a .toString()
:
String expressionNew = newExpression.subSequence(0, expressionLength).toString();
But it doesn't work! The app compiles but when I press my backspace button the app stops and terminal points out the following exception:
FATAL EXCEPTION: main
java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=-2
at java.lang.String.startEndAndLength(String.java:583)
at java.lang.String.substring(String.java:1464) [...]
Thanks!