Fix Stack RPN Calculator Codes - Help Needed

  • Thread starter ali11
  • Start date
  • Tags
    Calculator
In summary: Then your answer will be 2.Then you should look for the handling code when the enter button is pressed. That is where the calculation should be happening. If nothing is happening, it could be because the code is not implemented correctly or there is a logical error causing the program to not execute the calculation.
  • #1
12
0
I have no idea what wrong my cods when I press = button of my calculator nothing happens.
can someone help me to fix my codes thanks.

Code:
import java.util.Stack;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import java.math.*;
 import java.util.Vector;
 

public class Ahsan extends JFrame{
 Stack s = new Stack();
 
private JFrame f;
 private JPanel p1,p2;
 private JTextField jtfMain;
 private JButton jbtnOn, jbtnMRC, jbtnMminus, jbtnMadd, jbtnDivide, jbtnOff, jbtnSeven, jbtnEight, jbtnNine, jbtnMultiply,
 jbtnSqrt, jbtnFour, jbtnFive, jbtnSix, jbtnMinus, jbtnSq, jbtnOne, jbtnTwo, jbtnThree, jbtnPlus, jbtn1x, jbtnZero, jbtnDecimal,
 jbtnEqual, jbtnEnter;
 
char ch;
 Stack<String> cStack=new Stack<String>();
 public Ahsan() {
 


setSize(400,190);
 setTitle("Reverse Polish Notation Calculator");
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setLayout(new BorderLayout());
 
jtfMain = new JTextField("");
 
jbtnOn = new JButton("ON/AC");
 jbtnMRC = new JButton("MRC");
 jbtnMminus = new JButton("M-");
 jbtnMadd = new JButton("M+");
 jbtnDivide = new JButton("/");
 jbtnOff = new JButton("OFF");
 jbtnSeven = new JButton("7");
 jbtnEight = new JButton("8");
 jbtnNine = new JButton("9");
 jbtnMultiply = new JButton("X");
 jbtnSqrt = new JButton("SQRT");
 jbtnFour = new JButton("4");
 jbtnFive = new JButton("5");
 jbtnSix = new JButton("6");
 jbtnMinus = new JButton("-");
 jbtnSq = new JButton("SQ");
 jbtnOne = new JButton("1");
 jbtnTwo = new JButton("2");
 jbtnThree = new JButton("3");
 jbtnPlus = new JButton("+");
 jbtn1x = new JButton("1/x");
 jbtnZero = new JButton("0");
 jbtnDecimal = new JButton(".");
 jbtnEqual = new JButton("=");
 jbtnEnter = new JButton("ENTER");
 
p1 = new JPanel();
 p1.setLayout(new GridLayout(1,1));
 
p2 = new JPanel();
 p2.setLayout(new GridLayout(5,5));
 
p1.add(jtfMain);
 
p2.add(jbtnOn);
 p2.add(jbtnMRC);
 p2.add(jbtnMadd);
 p2.add(jbtnMminus);
 p2.add(jbtnDivide);
 p2.add(jbtnSeven);
 p2.add(jbtnEight);
 p2.add(jbtnNine);
 p2.add(jbtnMultiply);
 p2.add(jbtnSqrt);
 p2.add(jbtnFour);
 p2.add(jbtnFive);
 p2.add(jbtnSix);
 p2.add(jbtnMinus);
 p2.add(jbtnSq);
 p2.add(jbtnOne);
 p2.add(jbtnTwo);
 p2.add(jbtnThree);
 p2.add(jbtnPlus);
 p2.add(jbtn1x);
 p2.add(jbtnZero);
 p2.add(jbtnDecimal);
 p2.add(jbtnEqual);
 p2.add(jbtnEnter);
 p2.add(jbtnOff);
 
ButtonHandler buttonHandler=new ButtonHandler();
 jbtnOn.addActionListener(new ButtonHandler());
 jbtnMRC.addActionListener(new ButtonHandler());
 jbtnMminus.addActionListener(new ButtonHandler());
 jbtnDivide.addActionListener(new ButtonHandler());
 jbtnOff.addActionListener(new ButtonHandler());
 jbtnSeven.addActionListener(new ButtonHandler());
 jbtnEight.addActionListener(new ButtonHandler());
 jbtnNine.addActionListener(new ButtonHandler());
 jbtnMultiply.addActionListener(new ButtonHandler());
 jbtnSqrt.addActionListener(new ButtonHandler());
 jbtnFour.addActionListener(new ButtonHandler());
 jbtnFive.addActionListener(new ButtonHandler());
 jbtnSix.addActionListener(new ButtonHandler());
 jbtnMinus.addActionListener(new ButtonHandler());
 jbtnSq.addActionListener(new ButtonHandler());
 jbtnOne.addActionListener(new ButtonHandler());
 jbtnTwo.addActionListener(new ButtonHandler());
 jbtnThree.addActionListener(new ButtonHandler());
 jbtnPlus.addActionListener(new ButtonHandler());
 jbtn1x.addActionListener(new ButtonHandler());
 jbtnZero.addActionListener(new ButtonHandler());
 jbtnDecimal.addActionListener(new ButtonHandler());
 jbtnEqual.addActionListener(new ButtonHandler());
 jbtnEnter.addActionListener(new ButtonHandler());
 
add(p1, BorderLayout.NORTH);
 add(p2, BorderLayout.CENTER);
 
setVisible(true);
 }
 
public class enterHandler implements ActionListener
 {
 public void actionPerformed(ActionEvent evt)
 {
 int a;
 int b;
 
if (evt.getSource() == jbtnEnter)
 {
 //JButton hooked = (JButton) evt.getSource();
 
jtfMain.setText(jtfMain.getText() + " ");
 
String value = jtfMain.getText();
 
//if(value.lastIndexOf().equals("+"))
 //{
 String [] itemValue = value.split(" ");
 //System.out.println(itemValue.length);
 
for (int i=0;i<itemValue.length;i++)
 {
 cStack.push(itemValue[i]);
 }
 

if(cStack.pop().equals("+"))
 {
 
BigInteger first = new BigInteger(cStack.pop());
 BigInteger second = new BigInteger(cStack.pop());
 
first = first.add(second);
 cStack.push(first + "");
 }
 if(cStack.pop().equals("-"))
 {
 
BigInteger first = new BigInteger(cStack.pop());
 BigInteger second = new BigInteger(cStack.pop());
 
first = first.subtract(second);
 cStack.push(first + "");
 }
 
/*else if(cStack.pop().equals("-"))
 {
 BigInteger first = new BigInteger(cStack.pop());
 BigInteger second = new BigInteger(cStack.pop());
 
first = first.subtract(second);
 cStack.push(first + "");
 }*/
 //jtfMain.setText(cStack.pop());
 /*switch(ch)
 {
 case "/" : a = first.divide(second);
 }*/
 }
 }
 
}
 //}
 
public class ButtonHandler implements ActionListener
 {
 public void actionPerformed(ActionEvent evt)
 {
 JButton clicked = (JButton) evt.getSource();
 
if(clicked == jbtnOn)
 {
 jtfMain.setText("");
 }
 else if(clicked == jbtnOne)
 {
 jtfMain.setText(jtfMain.getText() + "1");
 //cStack.push(1);
 }
 else if(clicked== jbtnTwo)
 {
 jtfMain.setText(jtfMain.getText() + "2");
 //cStack.push(2);
 }
 else if(clicked== jbtnThree)
 {
 jtfMain.setText(jtfMain.getText() + "3");
 //cStack.push(3);
 }
 else if(clicked== jbtnFour)
 {
 jtfMain.setText(jtfMain.getText() + "4");
 //cStack.push(4);
 }
 else if(clicked== jbtnFive)
 {
 jtfMain.setText(jtfMain.getText() + "5");
 //cStack.push(5);
 }
 else if(clicked== jbtnSix)
 {
 jtfMain.setText(jtfMain.getText() + "6");
 //cStack.push(6);
 }
 else if(clicked== jbtnSeven)
 {
 jtfMain.setText(jtfMain.getText() + "7");
 //cStack.push(7);
 }
 else if(clicked== jbtnEight)
 {
 jtfMain.setText(jtfMain.getText() + "8");
 //cStack.push(8);
 }
 else if(clicked == jbtnNine)
 {
 jtfMain.setText(jtfMain.getText() + "9");
 //cStack.push(9);
 }
 else if(clicked == jbtnZero)
 {
 jtfMain.setText(jtfMain.getText() + "0");
 //add.numbers[];
 }
 else if(clicked == jbtnDivide)
 {
 jtfMain.setText(jtfMain.getText() + "/");
 //ch = '/';
 }
 
else if(clicked== jbtnMultiply)
 {
 jtfMain.setText(jtfMain.getText() + "X");
 
}
 else if(clicked== jbtnPlus)
 {
 jtfMain.setText(jtfMain.getText() + "+");
 //ch = 'a';
 }
 else if(clicked== jbtnMinus)
 {
 jtfMain.setText(jtfMain.getText() + "-");
 
}
 /*else if(clicked == jbtnEnter)
 {
 //cStack.push(numbers[0]);
 jtfMain.setText(jtfMain.getText() + " ");
 }*/
 else if(clicked == jbtnEqual)
 {
 jtfMain.setText(jtfMain.getText() + cStack.pop());
 }
 }
 }
 
public static void main(String[] args) {
 Ahsan main = new Ahsan();
 }
 }
 
Last edited by a moderator:
Physics news on Phys.org
  • #3
I wouldn't know the first thing about java. But I do know that RPN calculators do not have an equals sign. They do not use an equals sign; it has no part to play in their operation. So if everything else works for your calculator, except the equals sign, then perhaps you are worrying unnecessarily?
 
  • #4
Taking what NascentOxygen said into account, if all you did was press =, what did you expect to happen?

Instead of an = button, you should have an Enter button. With an RPN calculator, to add 5 and 3, you would do this:
Click 5
Click Enter
Click 3
Click +

The result should be 8.
 
  • #5
I do have enter button but when i press it nothing happens
 
  • #6
It sounds like you are not familiar with the code you are using.

To add two numbers, type these 4 keypresses:
5
enter
2
+
 
  • #7
I know how RPN calculator works but the problem is when I put numbers and press enter I don't get any result. thanks
 
  • #8
ali11 said:
I do have enter button but when i press it nothing happens

ali11 said:
I know how RPN calculator works but the problem is when I put numbers and press enter I don't get any result. thanks
What is supposed to happen when you press enter? And by that, I mean, what part of the code should you be looking at? Since this is code that you wrote, you should have some idea of how the pieces fit together.
 
  • #9
ali11 said:
I do have enter button but when i press it nothing happens
Because you have it commented out.
 
  • #10
To subtract two numbers it should be like this
7
enter
1
-
 
  • #11
Yes.
 

1. What is a stack RPN calculator?

A stack RPN calculator is a type of calculator that uses a stack data structure to store and manipulate operands and operators in order to perform mathematical calculations. RPN stands for Reverse Polish Notation, which is a way of representing mathematical expressions without the use of parentheses.

2. Why do I need to fix my stack RPN calculator codes?

If your stack RPN calculator codes are not working properly, it could result in incorrect calculations or errors. Fixing the codes will ensure that your calculator functions correctly and produces accurate results.

3. What are some common issues that may cause errors in stack RPN calculator codes?

Some common issues that may cause errors in stack RPN calculator codes include incorrect use of stack operations, incorrect placement of operands and operators, and not properly handling edge cases such as dividing by zero.

4. How do I fix my stack RPN calculator codes?

To fix your stack RPN calculator codes, you will need to carefully review and debug your code to identify and correct any errors. This may involve checking for syntax errors, logic errors, and implementing proper error handling.

5. Are there any resources available to help me fix my stack RPN calculator codes?

Yes, there are many resources available to help you fix your stack RPN calculator codes. These include online tutorials, forums, and programming communities where you can ask for help and advice. Additionally, there are plenty of books and courses on programming and data structures that can provide valuable information on fixing coding errors.

Suggested for: Fix Stack RPN Calculator Codes - Help Needed

Replies
22
Views
2K
Replies
4
Views
716
Replies
8
Views
1K
Replies
8
Views
970
Replies
1
Views
720
Replies
2
Views
622
Back
Top