为什么会出现NumberFormatException?

我在CodeChef平台中提出了一个问题,并且代码在我的IDE中执行得很好,而不是在CodeChef平台中。

当我使用Scanner代替Buffered Reader时,也会出现NoSuchElementException。

Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:542)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Codechef.main(Main.java:13)

这是问题的链接:https://www.codechef.com/problems/LAYERS

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        while (T>0)
        {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int N = Integer.parseInt(st.nextToken());
            int C = Integer.parseInt(st.nextToken());
            int L[] = new int [N];
            int B[] = new int [N];
            int c[] = new int [N];
            int maxL =0,maxB=0;
            for(int i = 0;i<N;i++)
            {
                st = new StringTokenizer(br.readLine());
                L[i] = Integer.parseInt(st.nextToken());
                B[i] = Integer.parseInt(st.nextToken());
                c[i] = Integer.parseInt(st.nextToken());
                if(maxL<L[i])
                    maxL = L[i];
                if(maxB<B[i])
                    maxB = B[i];
            }
            int Graph[][] = new int[maxL/2][maxB/2];
            for(int i=0;i<N;i++)
            {
                for(int j=0;j<L[i]/2;j++)
                {
                    for(int k=0;k<B[i]/2;k++)
                    {
                      Graph[j][k]= c[i];
                    }
                }
            }
            int result[] =new int [C];
            for(int i=0;i<maxL/2;i++)
            {
                for(int j=0;j<maxB/2;j++)
                {
                    if(Graph[i][j]>0)
                    {
                        result[Graph[i][j]-1]++;
                    }
                }
            }
            for(int i=0; i<C;i++)
            {
                System.out.print(result[i]*4+" ");
            }
            T--;
        }
    }
}

帮助我为什么我会收到错误消息。

daluoxi 回答:为什么会出现NumberFormatException?

如果我能数到第13行(在此处发布的代码中总是不稳定),则您的异常来自此行:

int T = Integer.parseInt(br.readLine());

br.readLine()在没有更多输入时返回null。由于这是您第一次调用readLine(),因此,您的输入为空。您的异常消息中的null意味着null已传递给parseInt(或字符串"null",但这不太可能)。

对您的错误的解释也与您从NoSuchElementException报告的Scanner相同。

提示:我知道您的变量来自一个问题语句,该语句使用大写的TNCLB。仍然在Java中,让Java命名约定胜出:对变量使用小写字母。因此,tcapitalT。变量名T特别令人困惑,因为T在泛型中经常用作类型参数。

本文链接:https://www.f2er.com/3003367.html

大家都在问