[Solved] How can I fix this calculator error? I’m a beginner [duplicate]

monalisa Asks: How can I fix this calculator error? I’m a beginner [duplicate]

Code:
//x^2 button
            JButton button_x_mult_x = new JButton("x^2");
            button_x_mult_x.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
            button_x_mult_x.setBounds(6, 125, 80, 50);
            frmCalculator.getContentPane().add(button_x_mult_x);
            button_x_mult_x.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String text = textField.getText().toString().trim();
                    float number = Float.parseFloat(text);
                    float x = number*number;
                    if(text.contains("+") || text.contains("-") || text.contains("x") || text.contains("/") == true){
                        textField.setText("Invalid input");
                    }
                    else if(text.equals("Invalid input") == true) {
                        textField.setText("0");
                    }               
                    else {
                        textField.setText(String.valueOf(x));
                    }
                }
            });

…It doesn’t work either else if.

Logically, when entering for example “2 + 3” we cannot use directly the x^2 button…and I get that error: Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “2 + 3”

I tried to resolve the error with this if:

Code:
if (text.contains("+") || text.contains("-") || text.contains("x") || text.contains("/") == true){ 
    textField.setText("Invalid input");
}

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.