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();)解析完成...我忘了帶來的方法,這一切.. 謝謝你幫助球員:))