This discussion is archived
1 2 Previous Next 23 Replies Latest reply: Oct 17, 2012 6:39 AM by 968683 RSS

Java Calculator HELP!

960425 Newbie
Currently Being Moderated
I am making a calculator for my school project and i finished and it doesn't give me the result. there isent a = button you insert your number and then you push the opperation that you want to do. please help me its due on friday.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame {
     
     JButton add ,subtract, multiply, divide;
     JTextField num1 , num2 ;
     JLabel result , enter1 , enter2 ;
     
     public Calculator () {
          setLayout (new GridBagLayout());
          GridBagConstraints c = new GridBagConstraints ();
          
          enter1 = new JLabel ("lst: ");
          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.gridx = 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;
          
          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 Data");
                    result.setForeground(Color.RED) ;
                    return;
                    
                    }
               try {
                    number2 = Double.parseDouble(num2.getText()) ;
                              }catch (NumberFormatException e) {
                              result.setText("illegal data");
                              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 0") ;
                                        result.setForeground(Color.RED);
                                   } else {
                                        double quotient = number1 / number2 ;
                                        result.setText(number1 + "/" + number2 + "=" + quotient);
                                        result.setForeground(Color.RED);
                                   }
                              }
                                   
                    }
               }
          public static void main (String args []) {
               Calculator gui = new Calculator ();
               gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               gui.setVisible(true);
               gui.setSize(250,175);
               gui.setTitle("Event Night Product!");
     }
          }
  • 1. Re: Java Calculator HELP!
    rukbat Guru Moderator
    Currently Being Moderated
    Moderator Action and Advice:
    (1) Your post has been moved from the Java Programming forum to the New-To-Java forum. You are still a novice.

    (2) Go back and read the Forum FAQ page again. You did read it, didn't you?
    In particular, review the section that is labelled "Are there any useful formatting options not shown on the sidebar..." as well as the sticky post announcement at the top of the New-To-Java forum:
    "Formatting Code"
    https://forums.oracle.com/forums/ann.jspa?annID=1389
    then go back and edit your post. Put your code into the proper tags.
  • 2. Re: Java Calculator HELP!
    Tolls Journeyer
    Currently Being Moderated
    And after you've done all that you might want to expand on "it doesn't give me the result".
    That is, what result do you see, with what inputs, and what result did you expect?
  • 3. Re: Java Calculator HELP!
    960425 Newbie
    Currently Being Moderated
    thats why i need help it dosent give me a result.
  • 4. Re: Java Calculator HELP!
    doremifasollatido Journeyer
    Currently Being Moderated
    For one thing, you don't seem to ever add the result label to the frame.
  • 5. Re: Java Calculator HELP!
    Tolls Journeyer
    Currently Being Moderated
    957422 wrote:
    thats why i need help it dosent give me a result.
    OK, so when you've added code tags someone might fancy looking at it...
  • 6. Re: Java Calculator HELP!
    960425 Newbie
    Currently Being Moderated
    i dont understand what you are telling me that i need to do.
  • 7. Re: Java Calculator HELP!
    rukbat Guru Moderator
    Currently Being Moderated
    957422 wrote:
    i dont understand what you are telling me that i need to do.
    Go back and read the advice you were given in the very first response.
    You were told to always format your code postings by using code tags.
  • 8. Re: Java Calculator HELP!
    939520 Explorer
    Currently Being Moderated
    When you put your code between { code } tags, it will appear properly indented as in the following example:
    try {
        number2 = Double.parseDouble(num2.getText()) ; 
    }catch (NumberFormatException e) {
        result.setText("illegal data");
        result.setForeground(Color.RED);
        return; 
    }
  • 9. Re: Java Calculator HELP!
    abillconsl Explorer
    Currently Being Moderated
    doremifasollatido wrote:
    For one thing, you don't seem to ever add the result label to the frame.
    I don't know if you meant that this would get it to work or whether it is just one error, but this will in fact "get it to work" as the OP wants.
  • 10. Re: Java Calculator HELP!
    doremifasollatido Journeyer
    Currently Being Moderated
    abillconsl wrote:
    doremifasollatido wrote:
    For one thing, you don't seem to ever add the result label to the frame.
    I don't know if you meant that this would get it to work or whether it is just one error, but this will in fact "get it to work" as the OP wants.
    I didn't run the code in any way. I just read the code and noticed the issue that I stated---I didn't know or check whether or not that was all that is needed to "get it to work". There might be other errors (or might not, I didn't study it). The original post does have actionPerformed spelled wrong, but I assume that the code that he's compiling has it spelled correctly, or it wouldn't compile.
  • 11. Re: Java Calculator HELP!
    abillconsl Explorer
    Currently Being Moderated
    doremifasollatido wrote:
    abillconsl wrote:
    doremifasollatido wrote:
    For one thing, you don't seem to ever add the result label to the frame.
    I don't know if you meant that this would get it to work or whether it is just one error, but this will in fact "get it to work" as the OP wants.
    I didn't run the code in any way. I just read the code and noticed the issue that I stated---I didn't know or check whether or not that was all that is needed to "get it to work". There might be other errors (or might not, I didn't study it). The original post does have actionPerformed spelled wrong, but I assume that the code that he's compiling has it spelled correctly, or it wouldn't compile.
    Right, saw that too - I compiled and ran it. There is an issue with the multiply button not showing correctly, so a little work would need to be done with the GBCs, but it will work with the fix you suggested.
  • 12. Re: Java Calculator HELP!
    doremifasollatido Journeyer
    Currently Being Moderated
    abillconsl wrote:
    Right, saw that too - I compiled and ran it. There is an issue with the multiply button not showing correctly, so a little work would need to be done with the GBCs, but it will work with the fix you suggested.
    Looking at the code, I see that the subtract button has gridx set twice (second line should set gridy [or second line could just be skipped, as gridy is already '2']), which is apparently messing up the multiply button.
  • 13. Re: Java Calculator HELP!
    968683 Newbie
    Currently Being Moderated
    I got all of the buttons working but it still wont give me a result
  • 14. Re: Java Calculator HELP!
    abillconsl Explorer
    Currently Being Moderated
    Did you add the result label to the frame - previous post? It worked when I tried it.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points