Nolan Kr Nolan Kr - 3 months ago 18
Java Question

How to delete a string from its length?

So hey guys, I'm creating a calculator because I searched on google what's a good starter program to create when learning java and calculator appeared. Yes I'm pretty new to java so please bear with me.

Here below is a snippet of what I wanted something to be done. I've got problems of trying to create a backspace button. This is the part where when I click the button then it will do something.

here is the value of my jtextfield. What I want the jbutton to do is to delete one character/number from the jtextfield. I tried
value = value.length() - 1;
and realized that I can't subtract 1 from a string lol. Does anyone have any idea and help a newbie out?

private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
String value = result.getText();
value = value.length() - 1;


Extra: ~ Oh and also, can somebody give me a tip on how, in the simplest way, so that when I click the
button from the calculator then it would add the first number(s) inputted and the second number(s) inputted. It would be pretty much appreciated! Thanks.


All you need to do is use substring():

String value = result.getText();    
String newValue = value.substring(0, value.length() - 1);