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?