Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Creating postfix calc need help

  1. Nov 15, 2011 #1
    can somebody help. i m getting following errors. i m creating postfix calc
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:204: int cannot be dereferenced
    Integer arg2=resultValue.pop();
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:205: int cannot be dereferenced
    resultValue.push(resultValue.pop()+arg2);
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:205: operator + cannot be applied to <any>,java.lang.Integer
    resultValue.push(resultValue.pop()+arg2);
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:205: int cannot be dereferenced
    resultValue.push(resultValue.pop()+arg2);
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:211: int cannot be dereferenced
    Integer arg2=resultValue.pop();
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:212: int cannot be dereferenced
    resultValue.push(resultValue.pop()-arg2);
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:212: int cannot be dereferenced
    resultValue.push(resultValue.pop()-arg2);
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:219: int cannot be dereferenced
    Integer arg2=resultValue.pop();
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:220: int cannot be dereferenced
    resultValue.push(resultValue.pop()*arg2);
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:220: int cannot be dereferenced
    resultValue.push(resultValue.pop()*arg2);
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:227: int cannot be dereferenced
    Integer arg2=resultValue.pop();
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:228: int cannot be dereferenced
    resultValue.push(resultValue.pop()-arg2);
    ^
    C:\Users\Hamza\Pictures\CalcGUIPanel.java:228: int cannot be dereferenced
    resultValue.push(resultValue.pop()-arg2);
    ^
    13 errors

    Tool completed with exit code 1
    Code (Text):
    import java.util.Stack;
    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    import javax.swing.event.*;
    import java.util.Scanner;


    public class CalcGUIPanel extends JPanel
    {


    //--\- Component referenced during execution

     private JTextField displayField; // display result / input.

     //--\- Variables representing state of the calculator

     private boolean startNumber = true; // true: num key next

     private int resultValue = 0; // result so far

     private String previousOp = "="; // previous operation

     Stack<String> cStack=new Stack<String>();

     public CalcGUIPanel()
     {

     //--\- Display field

     displayField = new JTextField();

    JButton clearButton = new JButton("CLEAR");



    clearButton.addActionListener(new ClearListener());



     //--\- One listener for all numeric keys.

     ActionListener numListener = new NumListener();

     //--\- Layout numeric keys in a grid. Generate the buttons

     // in a loop from the chars in a string.

     String buttonOrder = "789456123 0 ";

     JPanel buttonPanel = new JPanel(new GridLayout(5, 3));

     for (int i = 0; i < buttonOrder.length(); i++) {



     String keyTop = buttonOrder.substring(i, i+1);

     if (keyTop.equals(" ")) {

     buttonPanel.add(new JLabel(""));

     } else {

     JButton b = new JButton(keyTop);

     b.addActionListener(numListener);




     buttonPanel.add(b);

     }

     }


     //--\- One ActionListener to use for all operator buttons.

     ActionListener opListener = new OpListener();

     //--\- Create panel with gridlayout to hold operator buttons.

     // Use array of button names to create buttons in a loop.

     JPanel opPanel = new JPanel(new GridLayout(5, 1));

     String[] opOrder = {"+", "-", "*", "/", "enter"};

     for (int i = 0; i < opOrder.length; i++) {

     JButton b = new JButton(opOrder[i]);
      {

     cStack.push(opOrder[i]);
             }



     b.addActionListener(opListener);

    cStack.push("1");
    cStack.push("2");
    cStack.push("3");
    cStack.push("4");
    cStack.push("5");
    cStack.push("6");
    cStack.push("7");
    cStack.push("8");
    cStack.push("9");
    cStack.push("0");
     cStack.push("+");
      cStack.push("-");
      cStack.push("*");
      cStack.push("/");
      cStack.push("enter");




     opPanel.add(b);

     }

     //--\- Layout the top-level panel.

     this.setLayout(new BorderLayout());

     this.add(displayField, BorderLayout.NORTH );

     this.add(buttonPanel , BorderLayout.CENTER);

     this.add(opPanel , BorderLayout.EAST );

    this.add(clearButton , BorderLayout.SOUTH );

     }//end constructor

     //====================================================== action_clear

     /*\* Called by Clear btn action listener and elsewhere.*/

     private void action_clear() {

     startNumber = true;

     displayField.setText("0");

     resultValue = 0;
     int arg2;

     previousOp = "=";

     }

     // inner listener class OpListener

     /*\* Listener for all op buttons. \*/

     class OpListener implements ActionListener {

     public void actionPerformed(ActionEvent e) {

     // The calculator is always in one of two states.

     // 1. A number must be entered \-\- this operator is wrong.

     // 2. An operator must be entered \-\- we're ok.
     if (startNumber) { // Error: needed number, not operator

     action_clear();

     displayField.setText("ERROR");

     } else {



     startNumber = true; // Next thing must be a number


    try {

     String displayText = displayField.getText();

     int currentValue = Integer.parseInt(displayText);


     if (previousOp.equals("=")) {
     cStack.push(resultValue + "");





     }

      else if (previousOp.equals("+")) {
          Integer arg2=resultValue.pop();
             resultValue.push(resultValue.pop()+arg2);




     }  else if (previousOp.equals("-")) {
         Integer arg2=resultValue.pop();
         resultValue.push(resultValue.pop()-arg2);





     } else if (previousOp.equals("*")) {
        Integer arg2=resultValue.pop();
         resultValue.push(resultValue.pop()*arg2);



     } else if (previousOp.equals("/")) {
        cStack.pop().equals("/");

            Integer arg2=resultValue.pop();
         resultValue.push(resultValue.pop()-arg2);
     ;

     }

     displayField.setText("" + resultValue);
     } catch (NumberFormatException ex) {

     action_clear();

     displayField.setText("Error");

    }


     //--\- set \_previousOp for the next operator.

     previousOp = e.getActionCommand();

     }//endif \_startNumber

     }//endmethod

     }//end class

     //////////////////////////////////// inner listener class ClearListener

     // Action listener for numeric keys

     class NumListener implements ActionListener {

     public void actionPerformed(ActionEvent e) {

     String digit = e.getActionCommand(); // Get text from button

     if (startNumber) {

     // This is the first digit, clear field and set

     displayField.setText(digit);

     startNumber = false;

     } else {

     // Add this digit to the end of the display field

     displayField.setText(displayField.getText() + digit);

     }

     }

     }//end class

     //inner listener class ClearListener

    class ClearListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    action_clear();
    }
    }
    }
     
    Last edited by a moderator: Nov 15, 2011
  2. jcsd
  3. Nov 15, 2011 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    Re: need help

    you are more likely to get help if you say WHAT you want help with. There are a LOT of posts on this site and nearly ALL of them are because someone needs help, so "needs help" is a pretty useless subject line.
     
  4. Nov 15, 2011 #3
    Re: need help

    i already explain what i need help with in my post.
     
  5. Nov 15, 2011 #4

    phinds

    User Avatar
    Gold Member
    2016 Award

    Re: need help

    So I take it you think that everyone here reads every post? Dream on.
     
  6. Nov 15, 2011 #5
    Re: need help

    dude instead of argue can u just answer my question
     
  7. Nov 15, 2011 #6

    phinds

    User Avatar
    Gold Member
    2016 Award

    Re: need help

    No, I don't HAVE an answer to your question. I was trying to help you GET an answer to your question, but you seem to be missing the point.
     
  8. Nov 15, 2011 #7
    resultvalue is an integer, wich is primitive object wich can't have any methods such as pop().
    pop() should be used on a stack object, and shouldn't there be some stack object besides cstack to hold intermediate results?
     
  9. Nov 15, 2011 #8
    so where should i use pop method.i m sorry this is my 1st time i m using stack.
     
  10. Nov 16, 2011 #9
    you must apply the push and pop method to a stack.

    arg1 = cstack.pop()
    arg2 = cstack.pop()
    resultvalue = arg1 + arg2
    cstack.push(resultvalue)
     
  11. Nov 16, 2011 #10

    Mark44

    Staff: Mentor

    To the OP: For this code to work, two values must have been previously pushed onto the stack. The pop method throws EmptyStackException if you try to pop a stack that is empty.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Creating postfix calc need help
  1. C++: Postfix to Infix (Replies: 0)

Loading...