Fixing Errors in Postfix Calc Creation

  • Thread starter Thread starter ali11
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 5K views
ali11
Messages
12
Reaction score
0
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:
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:
on Phys.org


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.
 


i already explain what i need help with in my post.
phinds said:
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.
 


dude instead of argue can u just answer my question
phinds said:
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.

phinds said:
So I take it you think that everyone here reads every post? Dream on.
 


ali11 said:
dude instead of argue can u just answer my question

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.
 
resultvalue is an integer, which is primitive object which 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?
 
so where should i use pop method.i m sorry this is my 1st time i m using stack.
willem2 said:
resultvalue is an integer, which is primitive object which 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?
 
ali11 said:
so where should i use pop method.i m sorry this is my 1st time i m using stack.

you must apply the push and pop method to a stack.

arg1 = cstack.pop()
arg2 = cstack.pop()
resultvalue = arg1 + arg2
cstack.push(resultvalue)
 
willem2 said:
you must apply the push and pop method to a stack.

arg1 = cstack.pop()
arg2 = cstack.pop()
resultvalue = arg1 + arg2
cstack.push(resultvalue)

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.