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: How do I prevent these exceptions from occurring?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How do I prevent these exceptions from occurring?

    I am trying to make a calculator in Java. I have made much progress so far, but when the equal button is pressed when I try to do a calculation in the calculator, nothing happens and error messages regarding exceptions appear in the console. These are the error messages that appeared when I tried to carry out the calculation 2+2:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2+2"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1222)
    at java.lang.Double.parseDouble(Double.java:510)
    at Calculator$1Listener.actionPerformed(Calculator.ja va:124)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6414)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3275)
    at java.awt.Component.processEvent(Component.java:617 9)
    at java.awt.Container.processEvent(Container.java:208 4)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4776)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2142)
    at java.awt.Component.dispatchEvent(Component.java:46 04)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4618)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4279)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4209)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2128)
    at java.awt.Window.dispatchEventImpl(Window.java:2492 )
    at java.awt.Component.dispatchEvent(Component.java:46 04)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:717)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:86)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:690)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 687)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    This is my code:
    import javax.swing.*;//import the packages needed for gui
    import java.awt.*;
    import java.awt.event.*;
    import static java.lang.Math.*;
    public class Calculator {
    public static void main(String[] args) {
    JFrame window = new JFrame("Window");//makes a JFrame
        window.setSize(300,415); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel (new FlowLayout());//makes the panel, textfield and buttons and gives the buttons actioncommands
    final JTextField textField = new JTextField(20);
    JButton openbracket = new JButton("(");
        openbracket.setActionCommand("(");
    JButton closebracket = new JButton(")");
    closebracket.setActionCommand(")");
    JButton clearbutton = new JButton("C");
    clearbutton.setActionCommand("C");
    JButton arcsin = new JButton("arcsin");
    arcsin.setActionCommand("arcsin");
    JButton arccos = new JButton("arccos");
    arccos.setActionCommand("arccos");
    JButton arctan = new JButton("arctan");
    arctan.setActionCommand("arctan");
    JButton sin = new JButton("sin");
    sin.setActionCommand("sin");
    JButton cos = new JButton("cos");
    cos.setActionCommand("cos");
    JButton tan = new JButton("tan");
    tan.setActionCommand("tan");
    JButton log = new JButton("log");
    log.setActionCommand("log");
    JButton seven = new JButton("7");
    seven.setActionCommand("seven");
    JButton eight = new JButton("8");
    eight.setActionCommand("eight");
    JButton nine = new JButton("9");
    nine.setActionCommand("nine");
    JButton four = new JButton("4");
    four.setActionCommand("four");
        JButton five = new JButton("5");
        five.setActionCommand("five");
        JButton six = new JButton("6");
        six.setActionCommand("six");
        JButton one = new JButton("1");
        one.setActionCommand("one");
        JButton two = new JButton("2");
        two.setActionCommand("two");
        JButton three = new JButton("3");
        three.setActionCommand("three");
        JButton zero = new JButton("0");
        zero.setActionCommand("zero");
        JButton radixpoint = new JButton(".");
        radixpoint.setActionCommand("radixpoint");
        JButton plus = new JButton("+");
        plus.setActionCommand("plus");
        JButton subtract = new JButton("-");
        subtract.setActionCommand("subtract");
        JButton multiply = new JButton("x");
        multiply.setActionCommand("multiply");
        JButton divide = new JButton("/");
        divide.setActionCommand("divide");
        JButton equal = new JButton("=");
        equal.setActionCommand("equal");
        final String values = " ";
        class Listener implements ActionListener {  
            String out = "";
         public void actionPerformed(ActionEvent e) {             
            String output = e.getActionCommand();
            if(output == "(") {
               out = out + "(";
               textField.setText(out);   
            } else if (output == ")") {
            out = out + ")";
            textField.setText(out); 
            }   else if (output == "C") {
                textField.setText(" ");
                out = "";
            }  else if (output == "arcsin") {
                out = out + "asin(";
                textField.setText(out); 
            }   else if (output == "arccos") {
                out = out + "acos(";
                textField.setText(out);
            }    else if (output == "arctan") {
                out = out + "atan(";
                textField.setText(out);
            }   else if (output == "log") {
                out = out + "log(";
                textField.setText(out); 
            }   else if (output == "seven") {
                out = out + "7";
                textField.setText(out);
            }   else if (output == "eight") {
                out = out + "8";
                textField.setText(out);
            }   else if (output == "nine") {
                out = out + "9";
                textField.setText(out);
            }   else if (output == "four") {
                out = out + "4";
                textField.setText(out);
            }   else if (output == "five") {
                out = out + "5";
                textField.setText(out);
            }   else if (output == "six") {
                out = out + "6";
                textField.setText(out);
            }   else if (output == "one") {
                out = out + "1";
                textField.setText(out);
            }   else if (output == "two") {
                out = out + "2";
                textField.setText(out);
            }   else if (output == "three") {
                out = out + "3";
                textField.setText(out);
            }   else if (output == "zero") {
                out = out + "0";
                textField.setText(out);
            }   else if (output == "radixpoint") {
                out = out + ".";
                textField.setText(out);
            }   else if (output == "equal") {
                double val = Double.parseDouble(out);
                String str = String.valueOf(val);
                textField.setText(str);
            }   else if (output == "plus") {
                out = out + "+";
                textField.setText(out);
            }   else if (output == "subtract") {
                out = out + "-";
                textField.setText(out);
            }   else if (output == "multiply") {
                out = out + "*";
                textField.setText(out);
            }   else if (output == "divide") {
                out = out + "/";
                textField.setText(out);
            }
        }
    }
    Listener listener = new Listener(); //makes an object of the actionlistener
     panel.add(textField);//adding all the things
        window.add(panel);
    openbracket.addActionListener(listener);    
    panel.add(openbracket);
    closebracket.addActionListener(listener);
    panel.add(closebracket);
    clearbutton.addActionListener(listener);
    panel.add(clearbutton);
    arcsin.addActionListener(listener);
    panel.add(arcsin);
    arccos.addActionListener(listener);
    panel.add(arccos);
    arctan.addActionListener(listener);
    panel.add(arctan);
    sin.addActionListener(listener);
    panel.add(sin);
    cos.addActionListener(listener);
    panel.add(cos);
    tan.addActionListener(listener);
    panel.add(tan);
    log.addActionListener(listener);
    panel.add(log);
    nine.addActionListener(listener);
    panel.add(nine);
    eight.addActionListener(listener);
    panel.add(eight);
    seven.addActionListener(listener);
    panel.add(seven);
    six.addActionListener(listener);
    panel.add(six);
    five.addActionListener(listener);
    panel.add(five);
    four.addActionListener(listener);
    panel.add(four);
    three.addActionListener(listener);
    panel.add(three);
    two.addActionListener(listener);
    panel.add(two);
    one.addActionListener(listener);
    panel.add(one);
    zero.addActionListener(listener);
    panel.add(zero);
    radixpoint.addActionListener(listener);
    panel.add(radixpoint);
    plus.addActionListener(listener);
    panel.add(plus);
    subtract.addActionListener(listener);
    panel.add(subtract);
    multiply.addActionListener(listener);
    panel.add(multiply);
    divide.addActionListener(listener);
    panel.add(divide);
    equal.addActionListener(listener);
    panel.add(equal);
    window.setVisible(true);
    }
    }

    The expected behavior of the equal button is to convert the String variable out to double, then convert the calculated double variable back to String, and then output this String in the TextField. Instead, exceptions occur, preventing the expected behavior from happening. I suspect this is occurring because of an error in my coding. How do I prevent the exceptions from occurring, and what is wrong with my code?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I prevent these exceptions from occurring?

    For the exception:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2+2"

    The String, "2+2" is being used as an argument to the Double.parseDouble() method which is expecting something else, namely a valid double value. How you avoid the exception is to provide the parseDouble() method with the input it requires.

Similar Threads

  1. [SOLVED] Array Out Of Index Exception, Not Sure Why It's Occurring.
    By BlueEyesWhiteDragon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 31st, 2014, 05:44 PM
  2. Prevent object going outside of screen?
    By JakkyD in forum Java Theory & Questions
    Replies: 2
    Last Post: May 7th, 2012, 10:56 AM
  3. Top ten most frequently occurring characters program
    By mjclark91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 17th, 2011, 12:34 PM
  4. Random Errors Occurring When Running Game in Eclipse
    By WhenThCome4Me in forum Java IDEs
    Replies: 22
    Last Post: September 1st, 2011, 06:29 AM

Tags for this Thread