为什么我得到 java.lang.NumberFormatException: For input string: "∞"?

Why do I get java.lang.NumberFormatException: For input string: "∞"?

这是场景。 我根据从数据库中获取的数据动态生成要在 JPanel 上显示的组件。 提示用户输入一个整数,并根据该整数进行一些计算。输出应以十进制值形式给出。因此,我已将答案分配给双精度格式并使用 DecimalFormat 进行格式化。

当我将双精度值传递给 DecimalFormat 的 format() 方法时出现错误,尽管我没有输入任何值 0。

这是错误

java.lang.NumberFormatException: For input string: "∞" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at java.lang.Double.valueOf(Double.java:502) at com.boarding.brdrsbllsys.view.Bills.displayOutput(Bills.java:358) at com.boarding.brdrsbllsys.view.Bills.btnConfirmActionPerformed(Bills.java:186) at com.boarding.brdrsbllsys.view.Bills.access0(Bills.java:27) at com.boarding.brdrsbllsys.view.Bills.actionPerformed(Bills.java:123) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access0(EventQueue.java:97) at java.awt.EventQueue.run(EventQueue.java:709) at java.awt.EventQueue.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue.run(EventQueue.java:731) at java.awt.EventQueue.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

产生错误的代码片段,

if (gbc.gridx == 1 && gbc.gridy == i + 1) {
                textConstraints.gridx = 1;
                textConstraints.gridy = i + 1;
                newPanel.add(new JLabel(((JTextField) component).getText()), textConstraints);
                int days = Integer.parseInt(((JTextField) component).getText().trim());
                System.out.println("add num of days");

                DecimalFormat df = new DecimalFormat("#.##");

                textConstraints.gridx = 3;
                double a=water / waterdays * days*1.0;
                double boarderWater = Double.valueOf(df.format(a));//error is given in this line
                newPanel.add(new JLabel(boarderWater + ""), textConstraints);
                continue;

            }

请考虑变量gbctextConstraints是GridBagConstraints对象,water是double变量,waterdaysdays是int变量。

我的代码应该有什么问题?

您没有提供 waterwaterdaysdays 的值,但根据错误我只能得出 waterdays 为 0 的结论。

由于我们在这里使用双精度数,因此除以 0 不会像整数那样产生错误,而是会给出 Double.POSITIVE_INFINITY 作为结果。

这里有一个示例代码片段 waterdays = 0 来重现这个错误:

double water = 5.0;
int waterdays = 0;
int days = 3;

DecimalFormat df = new DecimalFormat("#.##");
double a = water / waterdays * days * 1.0;

double boarderWater = Double.valueOf(df.format(a));
System.out.println(boarderWater);

Try it online.

正如您在 TIO 调试 window 中看到的那样,格式化程序给出了相同的错误 java.lang.NumberFormatException: For input string: "∞",因为 waterdays 是 0。

请调试您的代码以检查 waterdays 的值。

简单说明:

除法过程中分母为零时出现此错误。

提出条件(使用 if else),即您的分母值不应为零。

或者如果你不能输入条件,

为分母的值为零设置异常处理,以避免您的应用程序崩溃。

使用

试试{//你的代码 }

catch(Exception e){//捕获异常 }