2014-01-25 29 views
0

我在做一个计算器,我得到一个NumberFormatExceptionNumberFormatException与计算器

这是我的方法的代码:

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) 
+0

getText()返回什么? –

+2

为什么要解析'parts [0]'两次?你有没有试过打印它的价值? – Pshemo

+0

当操作数是加法时,为什么你会乘以? – Obicere

回答

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]); 
+0

堆栈跟踪'NumberFormatException:对于输入字符串:“8 + 9”'他刚刚发布确认这是解决方案:) – Navin

2

的第一个参数是split正则表达式。如果你正在寻找一个加号,这个字符串不会完成这项工作,因为" + "意味着寻找一个或多个空格,然后是另一个空格。也就是说,它会查找2个或更多空格作为分隔符。所以如果你的输入字符串是"2 + 2",那么就没有2-space序列,所以split将会返回一个包含"2 + 2"的单元素数组作为字符串。这不是一个数字的正确格式。

要搜索一个空格,然后用加号后面加一个空格:

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

然后解决它,所以它当他们问到两个数相加不乘:) [OK,你这样做]

注:如果你最终想要让你的用户做除了另外的东西,你可能将无法使用split没有一个相当复杂的正则表达式。这是因为split不会返回分隔符。如果唯一可能的分隔符是+,那很好,但如果您可以有其他运营商,则需要知道运营商是什么,因此split将不起作用。您需要使用更一般的正则表达式匹配。看到这个tutorial

0

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

,如果你的想法是分析评估的表达,这样的任务的正确路径是

+0

从堆栈跟踪中,他试图分析'8 + 9'​​并在'+'上分割。 – jww

+0

然后他试图写出答案,所以他试图评估表达式 – Leo