I did a question in CodeChef platform and the code is getting executed well in my IDE and not in CodeChef Platform.
The same is the case when I use Scanner in place of Buffered Reader, I get 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)
Here is the link to the question : 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--;
}
}
}
Help me why I am getting the Error.
If I can count to line 13 (which is always shaky in code posted here), your exception is coming from this line:
int T = Integer.parseInt(br.readLine());
br.readLine()
returns null
when there is no more input. Since this is your first call to readLine()
, it means that your input is empty. The null
in your exception message means that a null
was passed to parseInt
(or the string "null"
, but that’s unlikely).
This explanation for your error also agrees with your reported NoSuchElementException
from Scanner
.
Tip: I know that your variables come from a problem statement that used T
, N
, C
, L
and B
in uppercase. Still in Java, let the Java naming conventions win: use lowercase for variables. So t
or capitalT
. The variable name T
is particularly confusing since T
is very often used as a type parameter in generics.