Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Please help with calculator program

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help with calculator program

    I'm trying to input multiple numbers but it only solves two numbers...


    public class Calculator extends JFrame{
    private JPanel panel;
    private JTextField display;
    private JButton[] button;
    static String lastCommand = null;
    double res;

    int tmp=0;
    boolean bool=false;

    private final String[] btn = {"1","2","3","4","5","6","7","8","9","0","C","+ ","-","*","/","="};
    public Calculator() {
    GridLayout gridLayout = new GridLayout(4,4,5,5);
    panel = new JPanel(gridLayout);
    display = new JTextField("0");

    display.setFont(new Font("Arial",Font.BOLD,40));
    display.setHorizontalAlignment(JTextField.RIGHT);
    display.setEditable(false);

    add(display,BorderLayout.NORTH);
    add(panel, BorderLayout.SOUTH);

    button = new JButton[16];
    HandlerClass handler = new HandlerClass();
    for(int i=0;i<btn.length;i++)
    {
    button[i] = new JButton(btn[i]);
    button[i].setForeground(Color.red);
    button[i].setFont(new Font("Arial",Font.BOLD,30));
    panel.add(button[i]);
    button[i].addActionListener(handler);
    }
    }
    private class HandlerClass implements ActionListener{
    public void actionPerformed(ActionEvent e) {

    String clickBtn = e.getActionCommand();

    if(clickBtn.equalsIgnoreCase("C"))
    {
    display.setText("0");

    }


    else if(clickBtn.equals("+"))
    {

    lastCommand="+";
    tmp = Integer.parseInt(display.getText());
    bool = true;

    }

    else if(clickBtn.equals("-"))
    {

    lastCommand="-";
    tmp = Integer.parseInt(display.getText());
    bool = true;

    }

    else if(clickBtn.equals("*"))
    {

    lastCommand="*";
    tmp = Integer.parseInt(display.getText());
    bool = true;

    }

    else if(clickBtn.equals("/"))
    {

    lastCommand="/";
    tmp = Integer.parseInt(display.getText());
    display.setText("0");

    }

    else if (clickBtn.equalsIgnoreCase("="))
    {


    double num2 = Integer.parseInt(display.getText());
    if(lastCommand.equals("+"))
    {
    display.setText(String.valueOf(num2+tmp));
    res=num2+tmp;
    }

    else if(lastCommand.equals("-"))
    {
    display.setText(String.valueOf(tmp-num2));
    res=tmp-num2;
    }

    else if(lastCommand.equals("*"))
    {
    display.setText(String.valueOf(num2*tmp));
    res=num2*tmp;
    }

    else if(lastCommand.equals("/"))
    {
    try{

    display.setText(String.valueOf(tmp/num2));
    if(num2 == 0)
    {
    display.setText("Error");
    }

    res=tmp/num2;
    }catch(ArithmeticException x)
    {
    display.setText("");
    }
    }

    }

    else
    {
    if(display.getText().equals("0") || bool==true)
    {
    display.setText("");
    bool = false;
    }
    display.setText(display.getText()+e.getActionComma nd());

    }

    }
    }

    }

    Please help thanks
    Last edited by dipizamora; August 30th, 2012 at 07:29 PM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Please help with calculator program

    Quote Originally Posted by dipizamora View Post
    This is the code... I'm trying to input multiple numbers but it only solves two numbers...
    Can you explain this in more detail? Explain it as if we don't have a clue as to anything about your program or how it works.

    You'll want to edit your post above and wrap the code in [code] [/code] tags so that it retains its formatting and is readable. Note the difference?:

    public class Calculator extends JFrame{
        private JPanel panel;
        private JTextField display;
        private JButton[] button;
        static String lastCommand = null;
        double res;
     
        int tmp=0;
        boolean bool=false;
     
        private final String[] btn = {"1","2","3","4","5","6","7","8","9","0","C","+","-","*","/","="};
        public Calculator() {
            GridLayout gridLayout = new GridLayout(4,4,5,5);
            panel = new JPanel(gridLayout);
            display = new JTextField("0");
     
            display.setFont(new Font("Arial",Font.BOLD,40));
            display.setHorizontalAlignment(JTextField.RIGHT);
            display.setEditable(false);
     
            add(display,BorderLayout.NORTH);
            add(panel, BorderLayout.SOUTH);
     
            button = new JButton[16];
            HandlerClass handler = new HandlerClass();
            for(int i=0;i<btn.length;i++)
            {
                button[i] = new JButton(btn[i]);
                button[i].setForeground(Color.red);
                button[i].setFont(new Font("Arial",Font.BOLD,30));
                panel.add(button[i]);
                button[i].addActionListener(handler);
            }
        }
        private class HandlerClass implements ActionListener{
            public void actionPerformed(ActionEvent e) {
     
                String clickBtn = e.getActionCommand();
     
                if(clickBtn.equalsIgnoreCase("C"))
                {
                   display.setText("0");
     
                }
     
     
                else if(clickBtn.equals("+"))
                {
     
                    lastCommand="+";
                    tmp = Integer.parseInt(display.getText());
                    bool = true;
     
                }
     
                else if(clickBtn.equals("-"))
                {
     
                    lastCommand="-";
                    tmp = Integer.parseInt(display.getText());
                    bool = true;
     
                }
     
                else if(clickBtn.equals("*"))
                {
     
                    lastCommand="*";
                    tmp = Integer.parseInt(display.getText());
                    bool = true;
     
                }
     
                else if(clickBtn.equals("/"))
                {
     
                    lastCommand="/";
                    tmp = Integer.parseInt(display.getText());
                    display.setText("0");
     
                }
     
                else if (clickBtn.equalsIgnoreCase("="))
                {
     
     
                     double num2 = Integer.parseInt(display.getText());
                     if(lastCommand.equals("+"))
                     {
                     display.setText(String.valueOf(num2+tmp));
                     res=num2+tmp;
                     }
     
                    else if(lastCommand.equals("-"))
                     {
                     display.setText(String.valueOf(tmp-num2));
                     res=tmp-num2;
                     }
     
                    else if(lastCommand.equals("*"))
                     {
                     display.setText(String.valueOf(num2*tmp));
                     res=num2*tmp;
                     }
     
                    else if(lastCommand.equals("/"))
                     {
                     try{
     
                     display.setText(String.valueOf(tmp/num2));
                        if(num2 == 0)
                        {
                            display.setText("Error");
                        }
     
                     res=tmp/num2;
                     }catch(ArithmeticException x)
                     {
                         display.setText("");
                     }
                     }
     
                }
     
                else
                {
                    if(display.getText().equals("0") || bool==true)
                    {
                        display.setText("");
                        bool = false;
                    }
                        display.setText(display.getText()+e.getActionCommand());
     
                }
     
            }
        }
     
    }

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help with calculator program

    I'm sorry. I'm just new here. It's confusing. Anyway, the problem with the program is that we are trying to solve multiple mathematical operations to input numbers but it only solves the first two numbers. For example: 2+2+2+2+2+2 = 4. That's the problem.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help with calculator program

    Which part of your code was written to handle the third number?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Please help with calculator program

    Can you edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting as shown in post#2
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Please help with calculator program

    Quote Originally Posted by jps View Post
    Which part of your code was written to handle the third number?
    And which parts were written by you vs which parts were borrowed from online sources? It's important for us to know which part of the code you fully understand and which parts you might not understand as well.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please help with calculator program

    Well, for addition shouldn't it be
    tmp += Integer.parseInt(display.getText());
    instead of
    tmp = Integer.parseInt(display.getText());
    ? Just had a quick look.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. calculator program
    By Milestone45 in forum Java Theory & Questions
    Replies: 5
    Last Post: April 15th, 2012, 03:28 PM
  2. Need Beginner Calculator Program help!
    By theJastro in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 17th, 2011, 07:30 PM
  3. [SOLVED] Calculator Program
    By mwardjava92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 3rd, 2011, 11:01 AM
  4. My Algebraic Calculator Program.
    By crazed8s in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 03:07 AM
  5. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM