0

我写了一个程序&我已经为它构建了一个GUI。当我尝试运行它,它抛出一个异常NumberFormatException,这里是我的一切:如何解析NumberFormatException当解析字符串为双/整数

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String 
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) 
    at sun.misc.FloatingDecimal.parseDouble(Unknown Source) 
    at java.lang.Double.parseDouble(Unknown Source) 
    at newtp.GUI$2.actionPerformed(GUI.java:219) 
    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$400(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) 

那么做我必须做的解决我的问题?

+1

只是不解析空字符串解析方法,它需要是一个有效的数字表示 – 2014-12-07 17:57:07

+0

把空支票 – Panther 2014-12-07 18:41:06

回答

0

你的问题是,你的字符串是空的

java.lang.NumberFormatException: empty String at 

为了解决这个问题,你应该填入一个数字的字符串表示(例如3串或7.7双)

字符串

如果你向我们展示你的代码,看看问题出在哪里,那将是最好的。

例子:

FF您做以下

String string1 = ""; 
int int1 = Integer.parseInt(string1); 

,您将收到您的Java以来​​接收到异常无法找到匹配的int值和空字符串。

如果你

String string2 = "3"; 
int int2 = Integer.parseInt(string2); 

您将不会收到一个异常,而是包含值3

0

一个解决方案的变量称为INT2,如Phiwa说,仅仅是不是在传递空字符串。不过,处理这个问题可能会更好。

您可以使用这样的事情如果输入的值是正确类型的未指定一个默认值:

int x; 
try { 
    //whatever you're doing to parse the string...probably x = Integer.parseInt("32"); or something 
catch(NumberFormatException e) { 
    e.printStackTrace(); 
    x = 12; //or whatever default value you want 
} 
+0

THX帮助:) – 2014-12-07 19:38:19

+0

@MoÕsKHälęd当然。如果这是正确的答案,请将其标记为正确,以便问题得到解决! – 2014-12-07 19:38:45

0

我已经铸造的变量时,它是空的,那是在这样的ActionListener我已经带来了价值(与textField.getText();)解析完成...我忘了带来的方法,这一切.. 谢谢你帮助球员:))