7 Replies Latest reply: Dec 1, 2006 1:41 AM by 800322 RSS

    Calculator - how do you enter double digit numbers with buttons?

    807599
      I have this calculator program that has buttons 0-9 on it. You click the buttons for whatever number you want just like any other calculator, but it only can do single digit numbers. For example, if I want to enter 15, it allows the 1, but then, the 5 can't be entered to make 15. How would I make it so it allows double digit numbers? Thanks in advance!
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      
      public class Calculator2 extends JFrame implements ActionListener
      {
           public static final int WIDTH = 800;
           public static final int HEIGHT = 150;
           public static final int NUMBER_OF_DIGITS = 30;
           private JTextField ioField;
           private int result = 0;
           private int first = 0;
           private int second = 0;
      
           public Calculator2()
           {
                setSize(WIDTH, HEIGHT);
                setTitle("GUI Calculator");
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setLayout(new BorderLayout());
      
                JPanel textPanel = new JPanel();
                textPanel.setLayout(new FlowLayout());
                ioField = new JTextField("Enter numbers here.", NUMBER_OF_DIGITS);
                ioField.setBackground(Color.WHITE);
                textPanel.add(ioField);
                add(textPanel, BorderLayout.NORTH);
      
                JPanel buttonPanel = new JPanel();
                buttonPanel.setBackground(Color.BLUE);
                buttonPanel.setLayout(new GridLayout(2, 10));
      
                JButton oneButton = new JButton("1");
                oneButton.addActionListener(this);
                buttonPanel.add(oneButton);
      
                JButton twoButton = new JButton("2");
                twoButton.addActionListener(this);
                buttonPanel.add(twoButton);
      
                JButton threeButton = new JButton("3");
                threeButton.addActionListener(this);
                buttonPanel.add(threeButton);
      
                JButton fourButton = new JButton("4");
                fourButton.addActionListener(this);
                buttonPanel.add(fourButton);
      
                JButton fiveButton = new JButton("5");
                fiveButton.addActionListener(this);
                buttonPanel.add(fiveButton);
      
                JButton sixButton = new JButton("6");
                sixButton.addActionListener(this);
                buttonPanel.add(sixButton);
      
                JButton sevenButton = new JButton("7");
                sevenButton.addActionListener(this);
                buttonPanel.add(sevenButton);
      
                JButton eightButton = new JButton("8");
                eightButton.addActionListener(this);
                buttonPanel.add(eightButton);
      
                JButton nineButton = new JButton("9");
                nineButton.addActionListener(this);
                buttonPanel.add(nineButton);
      
                JButton zeroButton = new JButton("0");
                zeroButton.addActionListener(this);
                buttonPanel.add(zeroButton);
      
                JButton clearButton = new JButton("C");
                clearButton.addActionListener(this);
                buttonPanel.add(clearButton);
      
                JButton addButton = new JButton("+");
                addButton.addActionListener(this);
                buttonPanel.add(addButton);
      
                JButton subButton = new JButton("-");
                subButton.addActionListener(this);
                buttonPanel.add(subButton);
      
                JButton multButton = new JButton("*");
                multButton.addActionListener(this);
                buttonPanel.add(multButton);
      
                JButton divButton = new JButton("/");
                divButton.addActionListener(this);
                buttonPanel.add(divButton);
      
                JButton expButton = new JButton("^");
                expButton.addActionListener(this);
                buttonPanel.add(expButton);
      
                JButton logButton = new JButton("v");
                logButton.addActionListener(this);
                buttonPanel.add(logButton);
      
                JButton sucButton = new JButton("s");
                sucButton.addActionListener(this);
                buttonPanel.add(sucButton);
      
                JButton preButton = new JButton("p");
                preButton.addActionListener(this);
                buttonPanel.add(preButton);
      
                JButton sqrtButton = new JButton("q");
                sqrtButton.addActionListener(this);
                buttonPanel.add(sqrtButton);
      
                JButton modButton = new JButton("%");
                modButton.addActionListener(this);
                buttonPanel.add(modButton);
      
                add(buttonPanel, BorderLayout.CENTER);
           }
      
           public void actionPerformed(ActionEvent e)
           {
                try
                {
                     assumingCorrectNumberFormats(e);
                }
                catch(NumberFormatException e2)
                {
                     ioField.setText("Error - Reenter number.");
                }
           }
      
           public void assumingCorrectNumberFormats(ActionEvent e)
           {
                String actionCommand = e.getActionCommand();
      
                if(actionCommand.equals("+"))
                {
                     result = Ops2.add(result, stringToInteger(ioField.getText()));
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("-"))
                {
                     result = Ops2.sub(result, stringToInteger(ioField.getText()));
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("*"))
                {
                     result = Ops2.mult(result, stringToInteger(ioField.getText()));
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("/"))
                {
                     result = Ops2.div(result, stringToInteger(ioField.getText()));
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("^"))
                {
                     result = Ops2.exp(result, stringToInteger(ioField.getText()));
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("v"))
                {
                     result = Ops2.log(result);
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("s"))
                {
                     result = Ops2.suc(result);
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("p"))
                {
                     result = Ops2.pre(result);
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("q"))
                {
                     result = Ops2.sqrt(result);
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("C"))
                {
                     result = 0;
                     ioField.setText("0");
                }
                else if(actionCommand.equals("%"))
                {
                     result = Ops.mod(result, stringToInteger(ioField.getText()));
                     ioField.setText(Integer.toString(result));
                }
                else if(actionCommand.equals("1"))
                {
                     ioField.setText("1");
                }
                else if(actionCommand.equals("2"))
                {
                     ioField.setText("2");
                }
                else if(actionCommand.equals("3"))
                {
                     ioField.setText("3");
                }
                else if(actionCommand.equals("4"))
                {
                     ioField.setText("4");
                }
                else if(actionCommand.equals("5"))
                {
                     ioField.setText("5");
                }
                else if(actionCommand.equals("6"))
                {
                     ioField.setText("6");
                }
                else if(actionCommand.equals("7"))
                {
                     ioField.setText("7");
                }
                else if(actionCommand.equals("8"))
                {
                     ioField.setText("8");
                }
                else if(actionCommand.equals("9"))
                {
                     ioField.setText("9");
                }
                else if(actionCommand.equals("0"))
                {
                     ioField.setText("0");
                }
                else
                {
                     ioField.setText("Unexpected error.");
                }
           }
      
           public static int stringToInteger(String stringObject)
           {
                return Integer.parseInt(stringObject.trim());
           }
      
           public static void main(String[] args)
           {
                Calculator2 gui = new Calculator2();
                gui.setVisible(true);
           }
      }