为什么我会收到 NumberFormatException?
Why am I getting 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)
这是问题的 link: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--;
}
}
}
请帮我看看为什么会出现错误。
如果我能数到第 13 行(此处发布的代码总是摇摇欲坠),您的异常来自这一行:
int T = Integer.parseInt(br.readLine());
br.readLine()
returns null
当没有更多输入时。由于这是您第一次调用 readLine()
,这意味着您的输入为空。异常消息中的 null
表示 null
已传递给 parseInt
(或字符串 "null"
,但这不太可能)。
您对错误的解释也与您从 Scanner
报告的 NoSuchElementException
一致。
提示:我知道您的变量来自使用大写 T
、N
、C
、L
和 B
的问题陈述.仍然在 Java 中,让 Java 命名约定获胜:对变量使用小写字母。所以 t
或 capitalT
。变量名 T
特别令人困惑,因为 T
在泛型中经常用作类型参数。
我在 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)
这是问题的 link: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--;
}
}
}
请帮我看看为什么会出现错误。
如果我能数到第 13 行(此处发布的代码总是摇摇欲坠),您的异常来自这一行:
int T = Integer.parseInt(br.readLine());
br.readLine()
returns null
当没有更多输入时。由于这是您第一次调用 readLine()
,这意味着您的输入为空。异常消息中的 null
表示 null
已传递给 parseInt
(或字符串 "null"
,但这不太可能)。
您对错误的解释也与您从 Scanner
报告的 NoSuchElementException
一致。
提示:我知道您的变量来自使用大写 T
、N
、C
、L
和 B
的问题陈述.仍然在 Java 中,让 Java 命名约定获胜:对变量使用小写字母。所以 t
或 capitalT
。变量名 T
特别令人困惑,因为 T
在泛型中经常用作类型参数。