Search code examples
javaadditioncalculatorbackspace

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. result 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;

            result.setText(value);
    }     

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.


Solution

  • String value = "abc";
    System.out.println(value.substring(0, value.length() - 1));
    

    Try this.