【问题标题】:java.lang.NumberFormatException: For input string: "", while not converting a string into a numberjava.lang.NumberFormatException:对于输入字符串:“”,而不是将字符串转换为数字
【发布时间】:2019-07-16 12:17:04
【问题描述】:

“当我单击保存按钮时出现 NumberFormatException 错误,但我没有将字符串转换为数字。”

我试过了:

try {
result = Integer.parseInt(input);
} catch(Exception e) {
if(input == null || input.isEmpty()) { // case 1a
return 0; //case 1a
} 

try {
String sql = "INSERT INTO c_records (job_id,u_name,model,warranty,deposit,balance,date,status,technician,balance_paid,clearance_date) VALUES (?,?,?,?,?,?,?,?,?,?,?)";   


pst = connect.prepareStatement(sql);



pst.setInt(1, Integer.valueOf(jTextField1.getText()));
pst.setString(2, jTextField2.getText());
pst.setString(3, jTextField3.getText());
pst.setInt(4, Integer.valueOf(jTextField4.getText()));
pst.setInt(5, Integer.valueOf(jTextField5.getText()));
pst.setInt(6, Integer.valueOf(jTextField6.getText()));
pst.setString(7, jTextField7.getText());
pst.setString(8, jTextField8.getText());
pst.setString(9, jTextField9.getText());
pst.setInt(10, Integer.valueOf(jTextField10.getText()));
pst.setString(10, jTextField10.getText());
pst.setString(11, jTextField11.getText());



pst.execute();

JOptionPane.showMessageDialog(null, "Record Saved!");
        }
        catch(Exception e)
           {
           JOptionPane.showMessageDialog(null, e);
           }

我想要发送按钮将数据发送到数据库。

【问题讨论】:

  • “我没有将字符串转换为数字” - 您正在调用 Integer.parseIntInteger.valueOf,它们都将字符串转换为数字...
  • 如果我将它们设置为字符串,它会带来错误。
  • 如何将整数值发送到数据库。
  • 嗯,大概您的一个文本字段是空的 - 那时您想做什么?
  • @JonSkeet,问题是如果文本字段为空,提问者希望将值设置为 0 - 请参见第一段代码中的案例 1a。目前还不清楚该块何时被调用,因为如果它防止为空,它应该可以工作。 pol,当你说你尝试了第一个区块时,你是在什么阶段尝试的?

标签: java runtime-error runtimeexception


【解决方案1】:

我在 catch 参数中添加了这个

 catch(HeadlessException | NumberFormatException | SQLException e)

我输入数据时日期格式也错误,所以我更改了它。

最后,我删除了:

pst.setString(10, jTextField10.getText());

【讨论】:

    【解决方案2】:

    注意整数在 -2,147,483,648 到 +2,147,483,647 的范围内

    如果您传递任何大于整数或包含字符的字符串,则会收到该错误;

    案例一

    Integer.parseInt("1232322132434")
    

    错误:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1232322132434"
    

    案例 2:

    Integer.parseInt("1232w")
    
    
    Exception in thread "main" java.lang.NumberFormatException: For input string: "1232w"
    

    【讨论】:

    • 还要避免特殊字符。不要输入像 32,567 这样的余额
    • 请注意,在标题中,用户遇到了将空字符串传递给 parseInt 的问题。但不清楚的是,用户似乎也尝试过检查空字符串,这应该有所帮助。
    • 我建议 Pol 应该使用 e.printStackTrace() 函数来回显他的问题的确切来源。有错误的实际行
    【解决方案3】:

    所以你尝试的应该工作,但看起来你一定没有在你思考的时候调用它(也许是因为你没有想到 valueOf 调用 parseInt)。你可以把它写成一个小函数,比如:

    public static Integer safeValueOf(String s) {
        Integer result = null; // signals error of unanticipated kind
        try {
            result = Integer.parseInt(s);
        } catch (NumberFormatException e) {
            if (s == null || s.isEmpty()) { // case 1a
                result = 0; //case 1a
            }
        } finally {
            return result;
        }
    

    然后将pst.setInt(4, Integer.valueOf(jTextField4.getText())); 之类的行替换为

    pst.setInt(4, safeValueOf(jTextField4.getText()));
    

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 2013-08-06
      相关资源
      最近更新 更多