I am creating a Java swing calculator. I want to get the reciprocal of a given number. So if I input 0, it should print out an error message.
Here's my code:
public class Calculator extends JFrame implements ActionListener {
    double num=0, num2=0;
    String operator;
    JButton bReciprocal=new JButton("1/x");
    JTextField result=new JTextField("0", 25);
    public void actionPerformed(ActionEvent e) {
        String command=e.getActionCommand();
        if(command=="1/x") {
            try {
                num=1/num;
                result.setText(Double.toString(num));
            }
            catch(ArithmeticException ae) {
                result.setText("Math Error");
                num=0;
            }
        }
    }
}
However, if I give 0 as the input, what I get is infinity. What's wrong with this code? How can I make it show "Math error" instead of infinity?
 
     
    