Hi Guys,
I'm new to Java and hoping someone can help me with this code.
I started to work on this exercise to create a calculator app, and Im really stuck on two things:

1. NumberFormatExeption - NetBeans IDE keep show me this error:  

  }catch(NumberFormatExeption e){
  required: Throwable
  found:    NumberFormatExeption


2. I can't figure it out how to assign the values from JTextField num1, num2 to double numbe1 and number2

Here is the code, the part Im struggling with is marked with stars:
Thanks in advance,






                           package calculator;


                                import java.awt.GridBagConstraints;
                                import java.awt.GridBagLayout;
                                import java.awt.event.ActionEvent;
                                import java.awt.event.ActionListener;
                                import javax.swing.JButton;
                                import javax.swing.JFrame;
                                import javax.swing.JLabel;
                                import javax.swing.JTextField;


                                public class Calculator extends JFrame {

                                  //declaring the variables
                                 JButton add, substract, multiply, devide, squere, avarage;
                                 JTextField num1, num2;
                                 JLabel resoult, enter1, enter2;

                                 //constructor
                                 public Calculator(){
                                    setLayout(new GridBagLayout());
                                    GridBagConstraints c = new GridBagConstraints();

                                    //enter first number
                                    enter1 = new JLabel("1st: ");
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 0;
                                    c.gridy = 0;
                                    add(enter1, c);

                                    num1 = new JTextField(16);
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 1;
                                    c.gridy = 0;
                                    c.gridwidth = 3;
                                    add(num1, c);

                                      //enter second number
                                    enter2 = new JLabel("2nd ");
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 0;
                                    c.gridy = 1;
                                    c.gridwidth = 8;
                                    add(enter2, c);

                                    num2 = new JTextField(16);
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 1;
                                    c.gridy = 1;
                                    c.gridwidth = 8;
                                    add(num2, c);

                                  add = new JButton("+"); 
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 1;
                                    c.gridy = 2;
                                    c.gridwidth = 1;
                                    add(add, c);

                                    substract = new JButton("-"); 
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 2;
                                    c.gridy = 2;
                                    add(substract, c);

                                    multiply = new JButton("*"); 
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 1;
                                    c.gridy = 3;
                                    add(multiply, c);

                                    devide = new JButton("/"); 
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 2;
                                    c.gridy = 3;
                                    add(devide, c);

                                    squere = new JButton("Sq"); 
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 3;
                                    c.gridy = 2;
                                    add(squere, c);

                                    avarage = new JButton("Av"); 
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 3;
                                    c.gridy = 3;
                                    add(avarage, c);

                                    resoult = new JLabel(" ");
                                    c.fill = GridBagConstraints.HORIZONTAL;
                                    c.gridx = 1;
                                    c.gridy = 5;
                                    c.gridwidth = 8;
                                    add(resoult, c);

                                    event callogic = new event();
                                    add.addActionListener(callogic);
                                    substract.addActionListener(callogic);
                                    multiply.addActionListener(callogic);
                                    devide.addActionListener(callogic);
                                    squere.addActionListener(callogic);
                                    avarage.addActionListener(callogic);
                                 }
                                public class event implements ActionListener{
                                    @Override
                                    public void actionPerformed(ActionEvent callogic){


                            / *             double number1 = 0 , number2 = 0; //Complirer set them to zero by 
                                                                              // default
                                                                            //I can figure out how to assign
                                                                            //values from num1 and num2




                            *             try{
                            *                 number1 = Double.parseDouble(num1.getText());
                            *             }catch(NumberFormatExeption e){
                            *                 resoult.setText("Illigal data 1st field");
                            *                 }
                            *              try{
                            *                 number2 = Double.parseDouble(num2.getText());
                            *             }catch(NumberFormatExeption e){
                            *                 resoult.setText("Illigal data 2nd field");
                                           }*/


                                         String op = callogic.getActionCommand();

                                           if(op.equals("+")){
                                        double sum = number1 + number2;
                                        resoult.setText(number1 + " + " + number2 + " = " + sum);
                                    }
                                          else if(op.equals("-")){
                                        double difference = number1 + number2;
                                        resoult.setText(number1 + " - " + number2 + " = " + difference);
                                    }
                                          else if(op.equals("*")){
                                        double multiplication = number1 * number2;
                                        resoult.setText(number1 + " * " + number2 + " = " + multiplication);
                                    }
                                             else if(op.equals("Sq")){
                                        double sque = number1 * number1;
                                        resoult.setText(number1 + "To Squere = " + sque);
                                    }
                                             else if(op.equals("Av")){
                                        double avar = number1 + number2 / 2;
                                        resoult.setText("The Avarage = " + avar);
                                    }
                                            else if(op.equals("/")){
                                              if(number2 == 0){
                                                  resoult.setText("Cannot devide by zero");
                                              }else{
                                        double division = number1 / number2;
                                        resoult.setText(number1 + " / " + number2 + " = " + division);
                                              }


                                            }
                                    }

Recommended Answers

All 2 Replies

mostly this exception will also comes with if you are passing empty string to Double.parseDouble("") method ( i mean while caliculating the result)

for finding this place s.o.p statements(to know what values are stored in those variables i mean num1.getText() & num2.getText()) before you are caliculating the results

try once that

i think **this is also one reason for getting that kind of exception every time **

happy coding

yours
radha krishna

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.