Results 1 to 7 of 7
  1. #1
    cukunit0 is offline Member
    Join Date
    Jun 2014
    Posts
    1
    Rep Power
    0

    Default Having stack calculator error

    I was cannot do the action with + - * /
    so i cannot have an output .
    where is the problem ?


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.util.Stack;
    
    public class JCalculator implements ActionListener {
    JFrame jfrm;
    JFrame jfrm2;
    JTextField txt;
    JLabel results;
    String str = "";
    Stack operands = new Stack();
    char[] a = new char[0];
    int used = 0;
    
    
    
    JCalculator(){
        jfrm = new JFrame("JCalc");
        jfrm.getContentPane().setLayout(new GridLayout(0,1));
        jfrm.setSize(210,210);
    
       results = new JLabel("",SwingConstants.RIGHT);
       jfrm.getContentPane().add(results);
       jfrm.setLocation(400,300);
       jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
       JButton[] calbut= new JButton[14];
       String []but = {"7","8","9","/","4","5","6","*","1","2","3","-","0","C"
       };
       JButton equal = new JButton("=");
       JButton add = new JButton("+");
    
       JPanel jbutton = new JPanel();
       jbutton.setLayout(new GridLayout(4,3));
       for(int i=0; i< but.length;i++){
           jbutton.add(calbut[i] = new JButton(but[i]));
    
           calbut[i].addActionListener(this);
       }
       jbutton.add(equal);
       jbutton.add(add);
    
       equal.addActionListener(this);
       add.addActionListener(this);
    
       jfrm.getContentPane().add(jbutton);
    
    
        jfrm.setVisible(true);
    }
    public void actionPerformed(ActionEvent ae){
        char[] a = new char[0];
        int used = 0;
        if(used == a.length){
            char[] newa= new char[a.length + 1];
            for(int i = 0; i <used; i++) newa[i]= a[i];
            a= newa;
        }
    
        if(ae.getActionCommand().equals("1")){
            str = results.getText();
            results.setText(str + "1");
            operands.push(1);
        }
    
        if(ae.getActionCommand().equals("2")){
            str = results.getText();
            results.setText(str + "2");
            operands.push(2);
        }
        if(ae.getActionCommand().equals("3")){
            str = results.getText();
            results.setText(str + "3");
            operands.push(3);
        }
        if(ae.getActionCommand().equals("4")){
            str = results.getText();
            results.setText(str + "4");
            operands.push(4);
        }
        if(ae.getActionCommand().equals("5")){
            str = results.getText();
            results.setText(str + "5");
            operands.push(5);
        }
        if(ae.getActionCommand().equals("6")){
            str = results.getText();
            results.setText(str + "6");
            operands.push(6);
        }
        if(ae.getActionCommand().equals("7")){
            str = results.getText();
            results.setText(str + "7");
            operands.push(7);
        }
        if(ae.getActionCommand().equals("8")){
            str = results.getText();
            results.setText(str + "8");
            operands.push(8);
        }
        if(ae.getActionCommand().equals("9")){
            str = results.getText();
            results.setText(str + "9");
            operands.push(9);
        }
        if (ae.getActionCommand().equals("0")) {
            str = results.getText();
            results.setText(str + "0");
            operands.push(0);
        }
        
         
        if(ae.getActionCommand().equals("+")){
            str = results.getText();
            double operand = Double.parseDouble(str);
            operands.push(operand);
            results.setText("");
            a[used]= '+';
            used++;
    
        }
        if(ae.getActionCommand().equals("-")){
            str = results.getText();
            double operand = Double.parseDouble(str);
            operands.push(operand);
            results.setText("");
            a[used]= '+';
            used++;
        }
        if(ae.getActionCommand().equals("/")){
            str = results.getText();
            double operand = Double.parseDouble(str);
            operands.push(operand);
            results.setText("");
            a[used]= '+';
            used++;
        }
        if(ae.getActionCommand().equals("*")){
            str = results.getText();
            double operand = Double.parseDouble(str);
            operands.push(operand);
            results.setText("");
            a[used]= '*';
            used++;
        }
        
        if(ae.getActionCommand().equals("C")){
            str = results.getText();
            double operand = Double.parseDouble(str);
            operands.push(operand);
            results.setText(" ");
           
        if(ae.getActionCommand().equals("=")){
    
            for(int i = 0; i< used; i++){
               performBinaryOp(a[i]);
            }
    
            str = String.valueOf(operands.pop());
            results.setText(str);
        }
    
    
    } 
        }
        
       
    public void performBinaryOp(char nextOperation) {
    
         double op1, op2;
        Double result = new Double(0);
        op1 = (Double) operands.pop();
        op2 = (Double) operands.pop();
        
    switch (nextOperation) {
                          
            case '+':
           
         result = new Double(op1 + op2);
         operands.push(result);
           break;
        case '-':
                result = new Double(op1 - op2);
                operands.push(result);
                break;
            case '*':
         result = new Double(op1 * op2);
         operands.push(result);
           break;
      case '/':
        result = new Double(op1 / op2);
        operands.push(result);
          break;
    
        }
       
    }
    
           
     
        
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JCalculator();                
            }            
        });        
    }
    }

    There are no function for the action there, once i click twice , it will show this.
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1011)
    at java.lang.Double.parseDouble(Double.java:540)
    at JCalculator.actionPerformed(JCalculator.java:134)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3320)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default Re: Having stack calculator error

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1011)
    at java.lang.Double.parseDouble(Double.java:540)
    at JCalculator.actionPerformed(JCalculator.java:134)
    At line 134 the code calls parseDouble() with an empty String.

    The code should test for the empty String BEFORE calling the parse method to keep this error from happening.


    The formatting of the {} is messed up at lines 166
    Last edited by Norm; 06-03-2014 at 03:14 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,029
    Rep Power
    6

    Default Re: Having stack calculator error

    Another observation. You have the following:
    Java Code:
    char[] a = new char[0];
    Defined in the class body and in your actionPerformed method. First, it defines an array of characters of size 0 which means it won't hold any. Second, you try to index it via some increasing variable 'i' in your actionPerformed method. This is definitely going to cause problems.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: Having stack calculator error

    Hm well perhaps that is not entirely wrong, the length of the 'a' array seems to be closely tied to the 'used' variable which also starts out as zero. The actionPerformed method seems to hint that the array is built to grow.

    What I find dubious is that both the 'a' and 'used' variables exist twice.

    Java Code:
    public class JCalculator implements ActionListener {
    JFrame jfrm;
    JFrame jfrm2;
    JTextField txt;
    JLabel results;
    String str = "";
    Stack operands = new Stack();
    char[] a = new char[0];            // 1
    int used = 0;
    Java Code:
    public void actionPerformed(ActionEvent ae){
        char[] a = new char[0];      // 2
        int used = 0;
    That doesn't seem right.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,029
    Rep Power
    6

    Default Re: Having stack calculator error

    I actually didn't follow the logic that closely. When I went back to look at it, it took me a few moments to also find
    this:

    a= newa;

    So 'a' is updated via a reference copy. So maybe there won't be a problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default Re: Having stack calculator error

    There are lots of problems. The code needs some debugging.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: Having stack calculator error

    Quote Originally Posted by Norm View Post
    There are lots of problems. The code needs some debugging.
    Yep. You can be cooking meat on the barbeque all day, there comes that time where you need to buckle down and clean the grill. It ain't going to happen by showing others how dirty it is :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Stack Based Calculator
    By Jeremy5 in forum New To Java
    Replies: 3
    Last Post: 11-05-2011, 08:57 AM
  2. java stack RPN calculator
    By ali1 in forum New To Java
    Replies: 0
    Last Post: 10-30-2011, 10:04 PM
  3. Stack Calculator
    By Bgreen7887 in forum New To Java
    Replies: 3
    Last Post: 01-29-2011, 12:01 AM
  4. Error stack trace
    By Arnold in forum Android
    Replies: 1
    Last Post: 05-13-2010, 06:54 PM
  5. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-20-2008, 12:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •