带计算器的NumberFormatException

时间:2014-01-25 01:46:34

标签: java numberformatexception

我正在制作一个计算器,我得到了NumberFormatException

这是我方法的代码:

String[] parts = text.getText().split(" + ", 2);
temporary[0] = Integer.parseInt(parts[0]);
temporary[1] = Integer.parseInt(parts[0]);
answer = temporary[0] + temporary[1];

这是我班级的代码:

public int answer = 0;
public int[] temporary = {0, 0};

我在这条线上得到了NFE:

temporary[0] = Integer.parseInt(parts[0]);

任何想法为什么?

这是我的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "8 + 9"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at main.Calculator.actionPerformed(Calculator.java:123)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

4 个答案:

答案 0 :(得分:5)

split使用正则表达式,因此split(" + ")将尝试拆分两个或更多个连续空格,因为您的字符串可能没有这样的空格,所以它不会被拆分,因此parts[0]将保留整个原始字符串。因此,您的代码将尝试解析类似

的内容
Integer.parseInt("123 + 321")` 

抛出NumberFormatException因为它不是正确的整数,这个方法可以解析。尝试在拆分中转义+。您也可以选择空格。

尝试

String[] parts = text.getText().split("\\s*\\+\\s*", 2);

另请注意,您尝试解析parts[0]两次。改变你的

temporary[1] = Integer.parseInt(parts[0]);

temporary[1] = Integer.parseInt(parts[1]);

答案 1 :(得分:2)

split的第一个参数是正则表达式。如果您正在寻找加号,则此字符串将无法执行此任务,因为" + "表示查找1个或多个空格,后跟另一个空格。也就是说,它查找2个或更多空格作为分隔符。因此,如果您的输入字符串是"2 + 2",则没有2空格序列,因此split将返回一个单元素数组,其中"2 + 2"为字符串。这不是数字的正确格式。

要搜索空格,后跟加号后跟空格:

String[] parts = text.getText().split(" \\+ ", 2);

然后修复它,以便当它们要求添加两个数字时它不会成​​倍增加:) [好的,你做到了。]

注意:如果你最终想让你的用户做一些除了添加之外的事情,你可能无法在没有相当复杂的正则表达式的情况下使用split。那是因为split不会返回分隔符。如果唯一可能的分隔符是+,那很好,但是如果你有其他运算符,你需要知道运算符是什么,所以split将不起作用。您需要使用更一般的正则表达式匹配。见tutorial

答案 2 :(得分:0)

文档说明原因。

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

  

抛出:

     

NumberFormatException - 如果字符串不包含可解析的整数。

答案 3 :(得分:0)

正如许多人在此指出的那样,您必须首先检查您要解析的字符串是否实际包含数字。

现在,如果您的想法是解析表达式以进行评估,那么此类任务的正确路径为