Java 将字符串解析为双精度/整数时如何解决NumberFormatException

Java 将字符串解析为双精度/整数时如何解决NumberFormatException,java,exception,user-interface,casting,numberformatexception,Java,Exception,User Interface,Casting,Numberformatexception,我已经编写了一个程序&我已经为它构建了一个GUI。当我尝试运行它时,它会抛出一个异常NumberFormatException,下面是我得到的全部信息: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at sun.misc.Flo

我已经编写了一个程序&我已经为它构建了一个GUI。当我尝试运行它时,它会抛出一个异常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)

那么我该怎么做才能解决我的问题呢?

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

java.lang.NumberFormatException: empty String at 
要解决此问题,应使用数字的字符串表示形式来填充字符串,例如,3表示字符串,7.7表示双精度

最好让我们看看你的代码,看看问题在哪里

例如:

如果您执行以下操作:

String string1 = "";
int int1 = Integer.parseInt(string1);
您将收到收到的异常,因为Java无法为和空字符串找到匹配的int值

如果你这样做

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

您不会收到异常,但会收到一个名为int2的变量,该变量包含值3。

正如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
}

我在actionListener中的变量为空时铸造了该变量,因此我使用textField.getText带来了值;解析已完成。。。我忘了带方法,仅此而已。。
感谢大家的帮助:

不要解析空字符串来解析方法,它需要是一个有效的数字representation@Mo当然可以。如果答案正确,请将其标记为正确,以便问题得以解决!