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 2 of 2

Thread: Calculator Throw Error

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculator Throw Error

    I'm working on creating a Calculator. The code is not finished yet but I'm getting an error already at a part I need to work in order to continue building the program.

    I can type in numbers fine, and when I use the addition button the first time it works. However, I'm trying to make it so when I input a number then press + then input another number and press + or = it will display the answer in the textfield. At this point if I try to press the + button twice it will give me this error:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "987561+"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
    at java.lang.Double.parseDouble(Double.java:510)
    at P18_10.Calculator$1ButtonListener.actionPerformed( Calculator.java:117)

    Line 117 in Calculator.java is:
    inputnumber = Double.parseDouble(input);

    Why would it have a problem converting a double to a String?

    Here is my code so far:


    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.GridLayout;
     
    public class Calculator extends JFrame {
     
        private JPanel ColorPanel;
        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 300;
        private ActionListener listener;
        private JTextField textField;
        double inputnumber = 0;
        String input = "0";
        double actionnumber = 0;
        double answer = 0;
        String action = null;
        String operation = "=";
     
        public Calculator() {
     
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            ColorPanel = new JPanel();
            add(ColorPanel, BorderLayout.CENTER);
            createButtons();
            createTextField();
     
        }
     
        public void createTextField() {
            JPanel northPanel = new JPanel();
            northPanel.setLayout(new BorderLayout());
     
            textField = new JTextField(29);
            textField.setEditable(true);
     
            northPanel.add(textField);
     
     
            add(northPanel, BorderLayout.NORTH);
     
        }
     
        public void createButtons() {
            JPanel southPanel = new JPanel();
     
            southPanel.setLayout(new GridLayout(4, 3));
            southPanel.add(makeButton("7", "7"));
            southPanel.add(makeButton("8", "8"));
            southPanel.add(makeButton("9", "9"));
            southPanel.add(makeButton("4", "4"));
            southPanel.add(makeButton("5", "5"));
            southPanel.add(makeButton("6", "6"));
            southPanel.add(makeButton("1", "1"));
            southPanel.add(makeButton("2", "2"));
            southPanel.add(makeButton("3", "3"));
            southPanel.add(makeButton("0", "0"));
            southPanel.add(makeButton(".", "."));
            southPanel.add(makeButton("CE", "CE"));
     
            southPanel.add(makeButton("+", "+"));
            southPanel.add(makeButton("-", "-"));
            southPanel.add(makeButton("X", "X"));
            southPanel.add(makeButton("/", "/"));
     
            add(southPanel, BorderLayout.CENTER);
     
        }
     
        public JButton makeButton(String label, final String action) {
            JButton button = new JButton(label);
            class ButtonListener implements ActionListener {
     
                public void actionPerformed(ActionEvent event) {
     
     
     
     
                    if (action.equals("=")) {
     
                        inputnumber = Double.parseDouble(input);
     
                        if (operation.equals("+")) {
                            answer = answer + inputnumber;
                            inputnumber = answer;
                            input = Double.toString(inputnumber);
                        }
     
     
     
                        if (input.equals("0")) {
                            input = action;
                        } else {
                            operation = "=";
                        }
     
                        input += action;
     
                        System.out.println("We are going to Add " + inputnumber + " To the Next Number");
     
     
     
                    } else if (action.equals("+")) {
     
                        inputnumber = Double.parseDouble(input);
     
                        if (operation.equals("+")) {
                            answer = answer + inputnumber;
                            inputnumber = answer;
                            input = Double.toString(inputnumber);
                        }
     
     
     
                        if (input.equals("0")) {
                            input = action;
                        } else {
     
                            operation = "+";
     
                            input += action;
     
                            System.out.println("We are going to Add " + inputnumber + " To the Next Number");
                        }
     
                    } else {
     
     
                        if (input.equals("0")) {
                            input = action;
                        } else {
                            input += action;
                        }
                    }
     
                    textField.setText(input);
     
     
                }
            }
            ButtonListener thelistener = new ButtonListener();
     
            button.addActionListener(thelistener);
            return button;
        }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Calculator Throw Error

    The problem is that you're parsing the whole string (including the plus). For example, "123+" is not a number, and Java is telling you that. What you could do is remove that last character, then you'd be left with "123", which Java can parse into a double.

    if(input.charAt(input.length()-1) == '+')
        inputnumber = Double.parseDouble(input.substring(0,input.length() - 1)); // untested code, should work though
    else
        inputNumber = Double.parseDouble(input);

Similar Threads

  1. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  2. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  3. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM
  4. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  5. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM