【问题标题】:unable to understand the cause of Number Format Exception无法理解数字格式异常的原因
【发布时间】:2012-02-09 11:49:03
【问题描述】:

我编写了一个小程序,其中用户输入分钟,程序显示当前日期和时间 + 用户输入的分钟。

final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);

Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, Integer.valueOf(sample.getMinutes()));

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
String dt = sdf.format(cal.getTime());

System.out.println(" Date and time with added Minutes : " + (dateFormat.parse(dt));

示例

private String minutes;

//getter and setter

我遇到了这个异常

java.lang.NumberFormatException:对于输入字符串:“”

我在这里做错了什么? 我应该使用

Integer.parseInt

或

Integer.valueOf(Integer.parseInt(sample.getMinutes())));?

【问题讨论】:

  • 什么是sample? NumberFormatException 的发生是因为您有一个空字符串 "" 试图将其解析为数字,但空字符串无法合理地解析为数字。 sample.getMinutes() 返回一个空字符串。
  • 你初始化了样本变量吗?

标签: java numberformatexception


【解决方案1】:
  1. 使用当前日期和时间。

    public static void main(String[] args) 
    {
        try 
        {
            final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
            final DateFormat dateFormat  = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
    
            Date sample  = new Date();
            int iMinutes = 30;//minutes added by the user
    
            Calendar cal = Calendar.getInstance();
    
            cal.add(Calendar.MINUTE, Integer.valueOf(sample.getMinutes()));
    
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            String dt            = sdf.format(cal.getTime());
    
            System.out.println("Current Date and time:"+sample);
            System.out.println("Date and time with added Minutes : " + (dateFormat.parse(dt)));
        } 
        catch (ParseException ex) 
        {
            Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    输出将显示为:
    当前日期和时间:Tue Jun 12 15:57:55 IST 2012
    添加分钟的日期和时间:2012 年 6 月 12 日星期二 16:54:55 IST
    此处将分钟“57”添加到日历中,时间向前移动了“30”分钟。这就是您的结果(添加分钟的日期和时间)。

  2. 用户输入分钟数。

    public static void main(String[] args) 
    {
        try 
        {
            final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
            final DateFormat dateFormat  = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
    
            int iMinutes = 30;//minutes added by the user
    
            Calendar cal = Calendar.getInstance();
    
            cal.add(Calendar.MINUTE, iMinutes);
    
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            String dt            = sdf.format(cal.getTime());
    
            System.out.println("Current Date and time:"+sample);
            System.out.println("Date and time with added Minutes : " + (dateFormat.parse(dt)));
        } 
        catch (ParseException ex) 
        {
            Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    这将根据您的意愿工作,首先您从用户那里获取分钟并将该分钟分配给代码的“iMinutes”变量,它将向日历添加那么多分钟。

    输出将显示为:
    当前日期和时间:Tue Jun 12 16:07:55 IST 2012
    添加分钟的日期和时间:Tue Jun 12 16:37:55 IST 2012

如果您想设置分钟,请在“cal.add”中使用“set”而不是“add”。

希望这能解决您的问题。
问候。

【讨论】:

    【解决方案2】:

    检查sample.getMinutes()返回的字符串是否为数字。它必须是一个没有任何空格的数字才能被解析,否则你会得到一个NumberFormatException。

    【讨论】:

      【解决方案3】:

      您遇到的问题是空字符串不是有效的整数。您的应用程序应该捕获异常,并设置一个合理的默认值。

      【讨论】:

        【解决方案4】:

        "" 是一个空字符串,在任何情况下都无法解析为有效整数

        【讨论】:

          【解决方案5】:

          java.lang.NumberFormatException:对于输入字符串:“”

          空字符串不能解析为数字。

          您需要先检查它(使用类似 String#length() 或 StringUtils#isBlank())并决定如何处理这种情况(例如将其视为零)。

          【讨论】:

            【解决方案6】:

            java.lang.NumberFormatException:对于输入字符串:“”

            好像你从来没有设置分钟字符串

            【讨论】:

              【解决方案7】:

              java.lang.NumberFormatException:对于输入字符串:“”

              输入字符串“”无法转换为有效整数。

              在使用 Integer.parseInt 之前,请确保通过以下方式获取整数。

              1.提供用于检查int的javascript验证

              或/和

              2.为检查非整数字符串提供服务器端验证

              另见how to avoid NumberFormatException

              【讨论】:

                【解决方案8】:

                添加某种检查:

                final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
                final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
                
                Calendar cal = Calendar.getInstance();
                final String minutes = sample.getMinutes()
                cal.add(Calendar.MINUTE, Integer.valueOf((minutes != null && !minutes.isEmpty()) ? minutes : 0);
                
                SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
                String dt = sdf.format(cal.getTime());
                
                System.out.println(" Date and time with added Minutes : " + (dateFormat.parse(dt));
                

                【讨论】:

                  【解决方案9】:

                  这里没有什么不寻常的地方。阅读parseInt() and valueOf 的java 文档,其中明确指出如果String 不包含parsable integer,则会抛出NumberFormatException。而empty string "" 不是可解析的 int。

                  如何处理抛出NumberFormatException 的情况取决于您。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-10-14
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-12-25
                    相关资源
                    最近更新 更多