Fixing Errors in Postfix Calc Creation

  • Thread starter ali11
  • Start date
In summary, the conversation is about a person asking for help with a code that is giving multiple errors while trying to create a postfix calculator. They are asking for help and someone suggests that they should clarify what exactly they need help with in order to get better assistance. The person asking for help then becomes defensive and asks for a direct answer instead of advice.
  • #1
ali11
12
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:
Technology news on Phys.org
  • #2


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.
 
  • #3


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.
 
  • #4


ali11 said:
i already explain what i need help with in my post.

So I take it you think that everyone here reads every post? Dream on.
 
  • #5


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.
 
  • #6


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.
 
  • #7
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?
 
  • #8
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?
 
  • #9
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)
 
  • #10
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.
 

Related to Fixing Errors in Postfix Calc Creation

1. How can I fix errors in my Postfix Calc creation?

To fix errors in your Postfix Calc, you will need to carefully review your code and identify where the error is occurring. Once you have identified the error, you can make the necessary changes to correct it. Additionally, you can use debugging tools or consult with other programmers for assistance.

2. What are some common errors that occur in Postfix Calc creation?

Some common errors in Postfix Calc creation include syntax errors, logic errors, and math errors. Syntax errors occur when the code is not written correctly, while logic errors occur when the code does not produce the intended result. Math errors occur when there is a mistake in the calculations being performed.

3. How can I prevent errors from occurring in my Postfix Calc creation?

To prevent errors in your Postfix Calc creation, it is important to write clean, well-structured code. This includes using proper syntax, breaking down complex operations into smaller, more manageable steps, and thoroughly testing your code before implementing it. Additionally, staying organized and documenting your code can also help prevent errors.

4. Can I use error handling techniques in Postfix Calc creation?

Yes, error handling techniques can be used in Postfix Calc creation. These techniques involve anticipating potential errors and implementing code to handle them gracefully. This can include displaying error messages, providing alternative calculations, or prompting the user for input to correct the error.

5. What resources can I use to learn more about fixing errors in Postfix Calc creation?

There are many resources available to help you learn more about fixing errors in Postfix Calc creation. These can include online tutorials, programming forums, and books on programming best practices. Additionally, consulting with experienced programmers or seeking help from a mentor can also be beneficial in improving your error-fixing skills.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top