【问题标题】:I am getting number format exception for this code我收到此代码的数字格式异常
【发布时间】:2015-12-28 07:11:06
【问题描述】:

我遇到了这个异常

线程“main”中的异常 java.lang.NumberFormatException: null
在 java.lang.Integer.parseInt(Integer.java:542)
在 java.lang.Integer.parseInt(Integer.java:615)
在 Test.main(Main.java:10)

代码:

import java.util.*;
import java.io.*; 
class Test {
        public static void main (String[] args) throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int a[]=new int[10],flag=1;
             System.out.println("Enter The Nos.");
            for(int i=0;i<=9;i++)
                a[i]=Integer.parseInt(br.readLine());
            int i=0;
            do{

                if(a[i]!=42)
                    System.out.println(a[i]);
                else{ flag=0;break;

                } i++;
            }while(flag==1);
        }
    }

它在 Eclipse 上运行良好,但在其他 IDE 上,它给出了这个异常。

【问题讨论】:

  • 你能告诉我你对这个程序的意见吗?
  • 异常信息已经够清楚了。你有什么不明白的?
  • 试试我回答中的代码。这应该可以解决问题。
  • 在尝试将其转换为整数之前尝试打印出“br.readLine()”的结果。这将为您提供有关如何修复程序的线索。

标签: java arrays exception numberformatexception


【解决方案1】:

问题在于这一行a[i]=Integer.parseInt(br.readLine());

在执行上述操作之前,您应该检查br.readLine() != null

您正在尝试将 null 解析为整数。这就是错误所暗示的。

编辑:
根据doc,如果到达流的末尾,readLine() 返回null

您的代码应如下所示:

import java.util.*;
import java.io.*;
class Test {
    public static void main (String[] args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a[]=new int[10],flag=1;
        System.out.println("Enter The Nos.");
        for(int i=0;i<=9;i++) {
            String num = br.readLine();
            if(num != null) {
                a[i]=Integer.parseInt(num);
            } else {
                //a[i] = 0;   // You might want to assign a[i] to some integer if the input number is null
            }
        }
        int i=0;
        do{

            if(a[i]!=42)
                System.out.println(a[i]);
            else{ flag=0;break; }
            i++;
        }while(flag==1 && i < 10);
    }
}

【讨论】:

  • 是的,但是为什么流只在 Eclipse 中为空?
  • 但请记住,如果 String 不包含可解析的 int,则会引发 NumberFormatException。包括 null ,这就是 Ashutosh 提到的情况:)
  • 这不是“即将到来的空流”。相反,它是 >>returning
  • @AadityaGavandalkar 在这种特殊情况下,它是因为 null 而发生的。 线程“main”中的异常 java.lang.NumberFormatException: null
  • 非常感谢伙计。但是现在,如果 a[i]!=42 对于完整数组为真,则会给出 ArrayIndexOutOfBoundsException。即如果数组中没有找到42,就会出现ArrayIndexOutOfBoundsException。
【解决方案2】:

Exception 描述性很好,您正在尝试将 null 解析为 int。在调用 parseInt() 之前做一个空值检查。检查 br 的空值能力就像

br.readLine() != null

然后解析它。

您的输入可能包含一个空格,因此 java 无法将其转换为数字,例如,“4 5”,为此您可以将所有空格替换为“”,然后尝试将其转换为数字。

br.readLine().replaceAll("\\s+", "");

您的代码将如下所示,

import java.util.*;
import java.io.*;
class Test {
    public static void main (String[] args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a[]=new int[10],flag=1;
        System.out.println("Enter The Nos.");
    if(br.readLine()!=null)
    {
        for(int i=0;i<=9;i++) {
            String num = br.readLine().replaceAll("\\s+", "");
            if(num != null) {
                a[i]=Integer.parseInt(num);
            } else {
                //a[i] = 0;   // You might want to assign a[i] to some integer if the input number is null
            }
        }
        int i=0;
        do{

            if(a[i]!=42)
                System.out.println(a[i]);
            else{ flag=0;break; }
            i++;
        }while(flag==1 && i < 10);
    }
}
}

【讨论】:

  • 非常感谢。但是这两个语句不起作用。也许我在这方面太穷了。您能否将这些语句放在我的代码中的正确位置
猜你喜欢
  • 2014-05-23
  • 2020-05-16
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2012-01-14
  • 1970-01-01
  • 2021-04-01
相关资源
最近更新 更多