简体   繁体   English

将字符串解析为double / integer时如何解决NumberFormatException

[英]How to solve NumberFormatException when parsing string to double/integer

I've written a program & I've built a GUI for it. 我已经编写了一个程序,并为此构建了一个GUI。 When I try to run it, it throws an exception NumberFormatException and here's all I got : 当我尝试运行它时,它将引发异常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)

So what do I have to do to solve my problem? 那么我该怎么做才能解决我的问题呢?

Your problem is that your string is empty 您的问题是您的字符串为空

java.lang.NumberFormatException: empty String at 

To solve this problem, you should fill the string with a string representation of a number (eg 3 for a string or 7.7 for a double) 要解决此问题,您应该用数字的字符串表示形式填充字符串(例如,字符串表示3或双精度表示7.7)

Would be best if you show us your code to see where the problem is. 如果您向我们展示代码以查看问题所在,那将是最好的。

Example: 例:

Ff you do the following 如果您执行以下操作

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

you will receive the exception you received since Java is not able to find the matching int value for and empty string. 您将收到您收到的异常,因为Java无法找到匹配的int值和空字符串。

If you do 如果你这样做

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

you won't receive an exception, but a variable called int2 containing the value 3. 您不会收到异常,但是会收到一个名为int2的变量,其中包含值3。

One solution, as Phiwa said, is just not to pass in an empty string. 正如Phiwa所说,一种解决方案是不传递空字符串。 It's probably better to handle the issue, though. 不过,处理该问题可能更好。

You can use something like this to assign a default value if the entered value isn't of the correct type: 如果输入的值类型不正确,则可以使用类似的方法分配默认值:

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();),解析完成了……我忘了引入所有方法了。为帮助大家:)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM