Java 计算器编程 NumberFormatException 的
Posted
技术标签:
【中文标题】Java 计算器编程 NumberFormatException 的【英文标题】:Java Calculator programming NumberFormatException's 【发布时间】:2014-09-26 08:20:45 【问题描述】:我正在尝试用 Java 制作计算器。到目前为止,我已经取得了很大的进步,但是当我尝试在计算器中进行计算时按下等号按钮时,没有任何反应,并且控制台中出现有关异常的错误消息。这些是我尝试进行 2+2 计算时出现的错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2+2"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Double.parseDouble(Double.java:510)
at Calculator$1Listener.actionPerformed(Calculator.java:124)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6414)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6179)
at java.awt.Container.processEvent(Container.java:2084)
at java.awt.Component.dispatchEventImpl(Component.java:4776)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
at java.awt.Container.dispatchEventImpl(Container.java:2128)
at java.awt.Window.dispatchEventImpl(Window.java:2492)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java: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.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(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.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
这是我的代码:
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);
我怀疑这是因为我的编码错误。谁能告诉我我的代码有什么问题以及为什么会发生这些异常?
【问题讨论】:
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。 了解如何读取 StackTrace 很重要。它说“2+2”不是一个数字。你同意吗 ?如果你这样做了,你应该明白你的错误。 您知道异常是java.lang.NumberFormatException
的一个实例,带有消息For input string: "2+2"
。为什么不查看 NumberFormatException
的 javadoc?
请把所有这些代码提取成 2 或 3 个更小的方法!!!!
0.02 -> 你重复了很多不必要的东西。对于if/else
链中的每种情况,只需使用out="theString";
将适当的字符串分配给out(不需要out=out+"someString";
)。然后在链结束后做textField.setText(out);
除非需要,否则总是尽量避免重复。此外,如果您使用的是 Java 7+,您可以switch
字符串。
【参考方案1】:
您不能将该 TextBox 中的所有内容解析为双精度。您只能解析仅包含数字的字符串..
试着这样做。
double num1 = Double.parseDouble(number1);
double num2 = Double.parseDouble(number2);
double val = num1 + num2;
【讨论】:
【参考方案2】:double val = Double.parseDouble(out);
如果您先将数字解析为双倍然后执行+
之类的数学运算,这可能会更好。解析器不知道如何将 String 2+2
解析为 double。
【讨论】:
以上是关于Java 计算器编程 NumberFormatException 的的主要内容,如果未能解决你的问题,请参考以下文章