Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Bug I cant Figure Out(Cant find the problem with my calculator application.)
Bug I cant Figure Out [message #1064093] Mon, 17 June 2013 15:22 Go to next message
Matthew Marion is currently offline Matthew MarionFriend
Messages: 1
Registered: June 2013
Junior Member
Hello! I recently just started java coding. I read a few tutorials on how to do basic things, such as hello world and other things. I tried to take it up a notch and i followed a calculator tutorial. I did everything, but whenever i click RUN, i get an error. Here is the code please help! Confused


calculator.java
code:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.MouseInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Calculator extends JFrame {

public JButton addButton;
public JButton subtractButton;
public JButton multiplyButton;
public JButton divideButton;
public JTextField firstNumber;
public JTextField secondNumber;
public JTextField thirdNumber;
public JTextField answer;
public JLabel answerLabel;


public Calculator () {
this.setTitle("Calculator by Omar S.");
this.setSize(new Dimension(250, 190));
this.setLocation(MouseInfo.getPointerInfo().getLocation());
this.setLayout(new FlowLayout());
this.setResizable(false);
this.addComponentListener(new ComponentListener() {
@OverRide
public void componentHidden(ComponentEvent arg0) {}
@Override
public void componentMoved(ComponentEvent arg0) {}
@Override
public void componentResized(ComponentEvent arg0) {}
@Override
public void componentShown(ComponentEvent arg0) {
Calculator_Load();

}

});

this.addButton = new JButton();
this.addButton.setText("Add");
this.addButton.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent e) {
addButton_ActionPerformed();

}


});

this.subtractButton = new JButton();
this.subtractButton.setText("Subtract");

this.subtractButton.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent e) {
subtractButton_ActionPerformed();

}


});
this.multiplyButton = new JButton();
this.multiplyButton.setText("Multiply");

this.multiplyButton.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent e) {
multiplyButton_ActionPerformed();

}


});
this.divideButton = new JButton();
this.divideButton.setText("Divide");

this.divideButton.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent e) {
divideButton_Actionperformed();

}


});

this.firstNumber = new JTextField();
this.firstNumber.setText(" ");

this.secondNumber = new JTextField();
this.secondNumber.setText(" ");

this.answer = new JTextField();
this.answer.setText(" ");
this.answer.setEditable(false);

this.answerLabel = new JLabel();
this.answerLabel.setText("Answer: ");

add(addButton);
add(subtractButton);
add(multiplyButton);
add(divideButton);
add(firstNumber);
add(secondNumber);
add(answerLabel);
add(answer);

}
private void Calculator_Load(){
firstNumber.setText("");
secondNumber.setText("");
answer.setText("");

}
private void addButton_ActionPerformed(){
try{
int num1, num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());

int answer2 = num1+num2;
answer.setText(String.valueOf(answer2));


}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Numbers");

}

}
private void subtractButton_ActionPerformed(){
try{
int num1, num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());

int answer2 = num1-num2;
answer.setText(String.valueOf(answer2));


}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Numbers");

}

}
private void multiplyButton_ActionPerformed(){
try{
int num1, num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());

int answer2 = num1*num2;
answer.setText(String.valueOf(answer2));


}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Numbers");

}

}
private void divideButton_Actionperformed(){
try{
int num1, num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());

int answer2 = num1/num2;
answer.setText(String.valueOf(answer2));


}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Numbers");

}

}


}


main.java
code:

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Calculator newform = new Calculator();
newform.setVisible(true);


}

}


OverRide.java
code:

public @interface OverRide {

}



Re: Bug I cant Figure Out [message #1064106 is a reply to message #1064093] Mon, 17 June 2013 16:24 Go to previous message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
The Eclipse forums are not for answering general Java programming questions. If you look at the sticky posts at the top of the forum, I believe that at least one topic points to general Java forums.

As a suggestion, you should always include the error message that you receive when asking a question on a forum. There are thousands of possible errors that can be reported when running a program. You shouldn't expect people you ask for help to guess at the error if you already know what it is.
Previous Topic:auto-search when tiping
Next Topic:Run configurations and commands being executed
Goto Forum:
  


Current Time: Fri Apr 26 14:55:54 GMT 2024

Powered by FUDForum. Page generated in 0.08826 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top