I tried to start using BufferedReader instead of Scanner. While coding for a question on codechef (SMRSTR), I tried taking space separated inputs by using StringTokenizer but it is raising exception i.e NumberFormatException. I found some question on StackOverflow regarding it but I think my problem is different, so I posted one.
Input: 1
2 3
2 3
5 100 8
I am getting:
Exception in thread "main" java.lang.NumberFormatException: For input string: "2 3"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at A.main(A.java:11)
I am getting first input t correctly from br.readLine();
But next inputs n,q
are giving the mentioned exception. I think the problem is in the nextToken from StringTokenizer, but still not getting it clearly.
Here is the code:
import java.io.*;
import java.util.*;
class A{
public static void main(String arg[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer s = new StringTokenizer(br.readLine());
int t= Integer.parseInt(br.readLine());
while(t-->0)
{
int n,q,i;
n=Integer.parseInt(s.nextToken());
q=Integer.parseInt(s.nextToken());
int D[]= new int[n];
int Q[]=new int[q];
long x=1;
for(i=0;i<n;i++)
{
D[i]=Integer.parseInt(s.nextToken());
x=x*D[i];
}
for(i=0;i<q;i++)
{
Q[i]=Integer.parseInt(s.nextToken());
if(x>1000000000)
Q[i]=0;
else
Q[i]=(int)(Q[i]/x);
}
for(i=0;i<q;i++)
System.out.print(Q[i]+" ");
System.out.println("");
}
}
}