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

    Default stack RPN calculaotr

    I have no idea what wrong with my cods when I press = button of my calculator nothing happens.can you guys help me fix my codes thanks.
    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();
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: stack RPN calculaotr

    Quote Originally Posted by ali1 View Post
    I have no idea what wrong with my cods ...
    Then your first task is to figure out where the problem is and then developing a plan for trying to fix it. What are you using to create and compile your code? If an IDE with a debugger such as Eclipse or NetBeans, then if I were you, I'd try to use the debugging features such as placing breakpoints in your code and inspecting variables at various spots in your program. If you are not using an IDE with a debugger, then sprinkle your code with println statements that tell you the state of certain variables at specific locations in the code. Then when you know what's wrong and if you still can't fix it, please come on back and let us know what you've found out.

    Luck!

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

    Default Re: stack RPN calculaotr

    I use netbeans. The only problem is that when I press enter button nothing happen.I don't get any result.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default Re: stack RPN calculaotr

    Quote Originally Posted by ali1 View Post
    I use netbeans. The only problem is that when I press enter button nothing happen.I don't get any result.
    Add some System.out.println( ... ) statements in the ActionListener that handles the ActionEvent for your enter button and see what your program is doing ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Stack
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 03-04-2011, 08:14 PM
  2. How big is the stack??
    By mgeno216 in forum New To Java
    Replies: 6
    Last Post: 03-04-2011, 08:04 AM
  3. Stack Calculator
    By Bgreen7887 in forum New To Java
    Replies: 3
    Last Post: 01-28-2011, 11:01 PM
  4. Heap or Stack
    By Metaferia in forum New To Java
    Replies: 3
    Last Post: 01-17-2011, 06:14 PM

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
  •