我正在制作一个计算器,但我收到了 NumberFormatException
。
这是我的方法的代码:
String[] parts = text.getText().split(" + ", 2);
temporary[0] = Integer.parseInt(parts[0]);
temporary[1] = Integer.parseInt(parts[0]);
answer = temporary[0] + temporary[1];
这是我的类(class)的代码:
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)
最佳答案
split
使用正则表达式,因此 split("+ ")
将尝试拆分两个或多个连续空格,并且由于您的字符串可能没有这样的空格,因此它会不会被分割,因此 parts[0]
将保存整个原始字符串。因此,您的代码将尝试解析类似的内容
Integer.parseInt("123 + 321")`
抛出 NumberFormatException
因为它不是此方法可以解析的正确整数。尝试在 split 中转义 +
。您还可以将空格设为可选。
尝试使用
String[] parts = text.getText().split("\\s*\\+\\s*", 2);
<小时/>
另请注意,您正在尝试解析 parts[0]
两次。改变你的
temporary[1] = Integer.parseInt(parts[0]);
到
temporary[1] = Integer.parseInt(parts[1]);
关于java - 计算器的 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21345516/