Results 1 to 3 of 3
  1. #1
    ali1 is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default can somebody help me

    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

    Java Code:
    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();
    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: can somebody help me

    Primitives do not have methods. You are doing something like:
    Java Code:
    int val = 10;
    val.doSomething();
    The pop method belongs to the Stack class. Check what you are trying to do.

  3. #3
    ali1 is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: can somebody help me

    i m creating rpn calc using stack.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •