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; } }