I am not able to understand why I am getting null from bufferedreader in following code (2nd line of output), while it worked fine at some places (1st line of output).
I have used several system.out.println's just for debugging purpose.
Although the BufferedReader.readLine() returns null only when the end of the stream is reached, the input is being provided (as shown in input below the program). Please help me in getting the reason of getting null and suggest a solution.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.lang.Integer;
import java.io.*;
class TestClass {
public static void main(String args[] ) throws Exception {
//* Read input from stdin and provide input before running
List a2=new ArrayList();
String[] a1=new String[2];
int count=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
/*for (String retval: line.split(" "))
a2.add(retval);*/
a1=line.split(" ");
//System.out.println("here 0"+a1[0]+" "+a1[1]);
/*int N = Integer.parseInt((a2.get(0)).toString());
int Q= Integer.parseInt((a2.get(1)).toString());*/
int N = Integer.parseInt(a1[0].toString());
int Q= Integer.parseInt(a1[1].toString());
System.out.println("here xxxxxxxx" + N +" " +Q);
String[] names=new String[N];
for(int i=0;i<N;i++){
//names[i] = (new BufferedReader(new InputStreamReader(System.in))).readLine();
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
names[i] = br1.readLine();
/*Scanner sc=new Scanner(System.in);
names[i]=sc.nextLine();*/
}
System.out.println("here 111" + names[0]);
for(int i=0;i<Q;i++) {
br = new BufferedReader(new InputStreamReader(System.in));
String line1 = br.readLine();
try{
System.out.println("here 1" + line1);
int M = Integer.parseInt(line1);
System.out.println("here 2");
if(M<=20){
System.out.println("here 3");
count++;
}
}
catch(Exception e){
System.out.println("here 4");
if(!((Arrays.asList(names)).contains(line))){
System.out.println("here 5");
count++;
}
}
}
System.out.println(count);
}
}
Input
First line of the input will contain two space separated integers denoting N and Q.
Next N lines will contain strings
Next Q lines will contain either an integer or a string denoting the name of a person. Different logics have to be implemented depending on whether it is aString or an Integer.
enter code here
Inputs and outputs are as follows:
Input:
2 4
pranjul
sachin
21
19
pranjul
vipul
Output:
here xxxxxxxx2 4
here 111null
here 1null
here 4
here 5
here 1null
here 4
here 5
here 1null
here 4
here 5
here 1null
here 4
here 5
4