Results 1 to 3 of 3

Thread: Error Message

  1. #1
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Error Message

    hi, im working on a calculator and im almost finished. All i need to do is add the = button's function, however this is what im strugling with. When ever the calc is run and i have the equation and i want to find the answer, e.g 3 + 7. I click the = button and nothing happens, in the background of my IDE i get lost of errors, heres my code.

    Java Code:
    public class Gui extends JFrame{
        
        public int ans;
        public String calc = "";
        public char[] number = {'0','1','2','3','4','5','6','7','8','9'};
        
        JTextField txt = new JTextField("", 30);
        
        public Gui() {
            
            setLayout(new FlowLayout());
            
            setTitle("Basic Calculator - Bimz");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(360, 130);
            setResizable(false);
            setLocationRelativeTo(null);
            
            txt.setEditable(false);
            add(txt);
            
            JButton btn0 = new JButton("0");
            add(btn0);
            
            JButton btn1 = new JButton("1");
            add(btn1);
            
            JButton btn2 = new JButton("2");
            add(btn2);
            
            JButton btn3 = new JButton("3");
            add(btn3);
            
            JButton btn4 = new JButton("4");
            add(btn4);
            
            JButton btn5 = new JButton("5");
            add(btn5);
            
            JButton btn6 = new JButton("6");
            add(btn6);
            
            JButton btn7 = new JButton("7");
            add(btn7);
            
            JButton btn8 = new JButton("8");
            add(btn8);
            
            JButton btn9 = new JButton("9");
            add(btn9);
            
            JButton btnP = new JButton("+");
            add(btnP);
            
            JButton btnM = new JButton("-");
            add(btnM);
            
            JButton btnE = new JButton("=");
            add(btnE);
            
                    
            JButton btnD = new JButton("DEL");
            add(btnD);
            
            HandlerClass0 handler0 = new HandlerClass0();
            HandlerClass1 handler1 = new HandlerClass1();
            HandlerClass2 handler2 = new HandlerClass2();
            HandlerClass3 handler3 = new HandlerClass3();
            HandlerClass4 handler4 = new HandlerClass4();
            HandlerClass5 handler5 = new HandlerClass5();
            HandlerClass6 handler6 = new HandlerClass6();
            HandlerClass7 handler7 = new HandlerClass7();
            HandlerClass8 handler8 = new HandlerClass8();
            HandlerClass9 handler9 = new HandlerClass9();
            
            btn0.addActionListener(handler0);
            btn1.addActionListener(handler1);
            btn2.addActionListener(handler2);
            btn3.addActionListener(handler3);
            btn4.addActionListener(handler4);
            btn5.addActionListener(handler5);
            btn6.addActionListener(handler6);
            btn7.addActionListener(handler7);
            btn8.addActionListener(handler8);
            btn9.addActionListener(handler9);
            
            HandlerClassP handlerP = new HandlerClassP();
            HandlerClassM handlerM = new HandlerClassM();
            HandlerClassD handlerD = new HandlerClassD();
            [COLOR="#0000FF"]HandlerClassE handlerE = new HandlerClassE();[/COLOR]
            
            btnP.addActionListener(handlerP);
            btnM.addActionListener(handlerM);
            btnD.addActionListener(handlerD);
            [COLOR="#0000FF"]btnE.addActionListener(handlerE);[/COLOR]
            
            setVisible(true);
            
        }
            
           [COLOR="#0000FF"] private class HandlerClassE implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    ans = Integer.parseInt(calc);
                    calc = Integer.toString(ans);
                    txt.setText(calc);
                    
                }
            }[/COLOR]
        
            private class HandlerClassD implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = "";
                    txt.setText(calc);
                    
                }
            }
        
            private class HandlerClassP implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + " + ";
                    txt.setText(calc);
                    
                }
            }
        
            private class HandlerClassM implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + " - ";
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass0 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[0];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass1 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[1];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass2 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[2];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass3 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[3];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass4 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[4];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass5 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[5];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass6 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[6];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass7 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[7];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass8 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[8];
                    txt.setText(calc);
                    
                }
            }
            
            private class HandlerClass9 implements ActionListener {
                 
                public void actionPerformed(ActionEvent event) {
    
                    calc = calc + number[9];
                    txt.setText(calc);
                    
                }
            }
    }

    the highlighted code is were im struggling i think, help is greatly appreciated.

    Thank You :)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Error Message

    Quote Originally Posted by Bimz View Post
    . When ever the calc is run and i have the equation and i want to find the answer, e.g 3 + 7. I click the = button and nothing happens, in the background of my IDE i get lost of errors, heres my code.
    Because ans = Integer.parseInt(calc); : calc is the whole string (3+7) that can not be parsed to an Integer! You have to split the string or use other methods like the Javascript-Engine :D But thats the wrong way!

  3. #3
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: Error Message

    so split it into 3 differant parts, '3' '+' '7' then do the calculation based on the differant parts?

Similar Threads

  1. 'else' without 'if' error message
    By jim01 in forum New To Java
    Replies: 6
    Last Post: 04-22-2011, 10:10 PM
  2. error message
    By tri.yudhanto in forum JDBC
    Replies: 0
    Last Post: 01-13-2011, 02:20 PM
  3. Error Message ..
    By Hamodi18 in forum New To Java
    Replies: 15
    Last Post: 07-11-2010, 04:31 AM
  4. Error Message????
    By Cubba27 in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 03:46 PM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 03:10 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
  •