**Hello i am a noob when it comes to java so sorry if its obvious but the code below is giving me a error when i try to compile it, the error is: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at goodcalculator.main(goodcalculator.java:127). Can anyone help me?
**
CODE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class goodcalculator extends JFrame {

    JButton add, subtract, multiply, divide;
    JtextField num1, num2;
    JLabel result, enter1, enter2;

    public goodcalculator() {
        setLayout (new GridBagLayout());
        GridBagConstraints c =new GridBagConstraints();

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

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

        enter2 = new JLabel ("2nd: ");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        add(enter2, c);

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

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

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

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

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

        result = new JLabel("");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 4;
        add(result, c);

        event a = new event();
        add.addActionListener(a);
        subtract.addActionListener(a);
        multiply.addActionListener(a);
        divide.addActionListener(a);        
    }

    public class event implements ActionListener {
        public void actionPreformed(ActionEvent a) {
            double number1, number2;

            try {
                number1 = Double.parseDouble(num1.getText());               
            } catch (NumberFormatException e) {
                result.setText("Illegal characters in first field");
                result.setForeground(Color.RED);
                return;
            }

            try {
                number2 = Double.parseDouble(num2.getTexr());
            } catch (NumberFormatException e) {
                result.setText("Illegal characters in second field");
                result.setForeground(Color.RED);
                return;
            }

            String op = a.getActionCommand();

            if(op.equals("+")) {
                double sum = number1 + number2;
                result.setText(number1 + "+" + number2 + "=" + sum);
                result.setForeground(Color.RED);                
            } else if(op.equals("-")) {
                double diff = number1 - number2;
                result.setText(number1 + "-" + number2 + "=" + diff);
                result.setForeground(Color.RED);                
            } else if(op.equals("*")) {
                double factor = number1 * number2;
                result.setText(number1 + "*" + number2 + "=" + factor);
                result.setForeground(Color.RED);
            } else if(op.equals("/")) {
                if(number2 == 0) {
                    result.setText("Cannot divide by zero");
                    result.setForeground(Color.RED);
                } else {
                    double quotient = number1 / number1;
                    result.setText(number1 + "/" + number2 + "=" + quotient);
                    result.setForeground(Color.RED);
                }
            }
        }
    }
    public static void main(String[] args) {
        goodcalculator gui = new goodcalculator();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.setSize(250,175);
        gui.setTitle("My very first java calculator");
    }
}

Recommended Answers

All 3 Replies

That means you tried to run the program when it still had error(s) that prevent it from compiling.
In Eclipse look for the little red x at the left of the code, red underlining under the code in question, and a red block in the vertical scroll bar to show you where to look. The text of the compiler message will appear when you hover the mouse over the rex x or the underlined code, or you can see all the messages in the Problems tab at the bottom of the screen.
At your level of experience it's probably not a good idea to use Eclipse - it's a tool for experts and is just going to add a lot more stuff to your learning curve. Consider using a programmmer's editor and the command line compiler javac for the moment.

Just check the spelling. Go through the whole code. Check the classes and initiation of variables. Will work.

Why do all that work? That's what compiler error messages are for.

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.