Why Does the Equal Button Not Work on My RPN Calculator?

  • Thread starter Thread starter ali11
  • Start date Start date
  • Tags Tags
    Calculator
Click For Summary
SUMMARY

The discussion centers on a malfunctioning equals button in a Reverse Polish Notation (RPN) calculator implemented in Java. Users clarify that RPN calculators do not utilize an equals sign; instead, they rely on an Enter button to process calculations. The original poster's code includes an Enter button, but it fails to produce results when pressed, indicating a potential issue in the event handling logic. The conversation emphasizes the need to ensure that the Enter button's action is correctly implemented to handle input and perform calculations.

PREREQUISITES
  • Understanding of Java programming, specifically Swing for GUI applications.
  • Familiarity with Stack data structures and their operations.
  • Knowledge of Reverse Polish Notation (RPN) and its operational principles.
  • Experience with event-driven programming in Java.
NEXT STEPS
  • Debug the actionPerformed method in the enterHandler class to ensure it processes inputs correctly.
  • Implement unit tests for the calculator's operations to verify the correctness of calculations.
  • Research Java Swing event handling to improve the responsiveness of the GUI.
  • Explore alternative designs for RPN calculators to enhance user experience and functionality.
USEFUL FOR

Java developers, software engineers working on calculator applications, and anyone interested in implementing or debugging RPN calculators.

ali11
Messages
12
Reaction score
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
Do not double post your questions.
 
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?
 
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.
 
I do have enter button but when i press it nothing happens
 
It sounds like you are not familiar with the code you are using.

To add two numbers, type these 4 keypresses:
5
enter
2
+
 
I know how RPN calculator works but the problem is when I put numbers and press enter I don't get any result. thanks
 
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.
 
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
-