Help write the algorithm for the calculator button. The button should delete the last (one) written element. For example:

Why is the last element not removed in this way?

String str; JTextField t1; str = t1.getText(); t1.setText(null); t1.setText(t1.setText() + (str.length() - 1)); 

And so without any problem you can delete the last character: Why can't I do this without substring ()?

  String str; JTextField t1; str = t1.getText(); t1.setText(null); str = str.substring(0, str.length() - 1); t1.setText(t1.getText() + str); 
  • According to community rules, questions should not be reduced to completing tasks for students. Give an example of your implementation and ask a question describing specific problems. - Nicolas Chabanovsky
  • You review your code again and think about what you do with each step: t1.setText(null); - assign a void to the text field, although this is not correct; to clear the field, do t1.setText(""); . Then t1.setText(t1.setText() + (str.length() - 1)); - you do not understand why you take t1.setText() : set in the "nothing" field and add to it, do not append, the length of the string is str minus 1. It will be more beautiful. The text field accepts a string . The substring(); method substring(); returns a string from the specified starting character, to the specified ending. - Pollux

1 answer 1

 String str = "string"; void modString(){ str = str.substring(0, str.length() - 1); System.out.println(str); } 

strin : strin