I
I
iRobot122016-10-27 14:53:58
Java
iRobot12, 2016-10-27 14:53:58

Calculator in Java, how to make a backspace button?

How to make a BackSpace button in a graphing calculator?

private void buttonBackspaceActionPerformed(java.awt.event.ActionEvent evt) {                                                
             String t = textField.getText();
            
             if (t.isEmpty()){
                   textField.setText("0");
             } else {
                  textField.setText(t.substring(0, t.length() - 1));  
             }
    }

But this code of mine, when you delete all the numbers, first gives out 0, when you press it again, it gives an empty string, and so on.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2016-10-27
@Rou1997

if (t.isEmpty() || t.getText().equals("0")){
PS Do you want a cross-platform calculator? If not, then why in Java?

T
Therapyx, 2016-10-28
@Therapyx

But this code of mine, when you delete all the numbers, first gives out 0, when you press it again, it gives an empty string, and so on.

well, if you use backspace on an "empty" field, then it should be so, you ask if the field is "empty", then make it 0. And if you click again, then it is no longer empty, and the else is called, where you cut 0 and the field becomes empty again. In theory, with this code - it should work as I described above))
try to add if (t.isEmpty() || t.getText() == "0")
maybe I'm already sleeping and stupid, then I'm sorry xD

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question