readLine() not working in BufferedReader for a java program

Hello, I have this simple java code where I’m taking input using BufferedReader class, here the method readLine() is not taking reading input at commented error line 1.
Could anyone plz check and help me out ?
– Thanks

code–


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

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

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br= new BufferedReader(new InputStreamReader( System.in));
		
	
		int n= br.read();
		int even=0; int odd=0;
		System.out.println();
		//int wep[] = new int[n];
		String  s2= br.readLine();  //  error line 1
		System.out.println("value of s2=  "+s2);
		
		try{
		for(int i=0; i<s2.length;i++)
		{  if(Integer.parseInt(s2[i])%2==0)
		    even++;
		    else odd++;
		    
		}}
		catch (Exception e)
		{ System.out.println("number format expected");} 
		
		if(even>odd)
		System.out.println("READY FOR BATTLE");
		else System.out.println("NOT READY");
	}
}

problem code- AMR15A

There are a bunch of compilation errors.

This shall be replaced with the following

for(int i = 0; i < s2.length(); i++)
***************************************************

And the following

Shall be replaced with

if(Integer.parseInt(s2.charAt(i)) % 2 == 0)

Hi, yes i know of those compilation errors as earlier I was doing this

String[] s2= br.readLine().split(" ");
which is why there is some confusion in s2 being a String or String array.

But my problem is that at the mentioned line "error line 1 " , no input is being read.
When you try to print out s2 after reading irrespective of its data type, s2 is always null which later causes exception in the program.

I’m trying to debug what functionality I’m missing regarding input reading procedure in when using BufferedReader class.

Could you plz explain that if aware ? thanks

Modified a bit.

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

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br= new BufferedReader(new InputStreamReader( System.in));
		
	
		int n= br.read();
		System.out.println(n);
		int even=0; int odd=0;
		System.out.println();
		//int wep[] = new int[n];
		String  s2= br.readLine();  //  error line 1
		s2= br.readLine();  //  error line 1
		System.out.println("value of s2=  "+s2);
		
// 		try{
// 		for(int i=0; i<s2.length();i++)
// 		{  if(Integer.parseInt(s2.charAt(i))%2==0)
// 		    even++;
// 		    else odd++;
		    
// 		}}
// 		catch (Exception e)
// 		{ System.out.println("number format expected");} 
		
// 		if(even>odd)
// 		System.out.println("READY FOR BATTLE");
// 		else System.out.println("NOT READY");
	}
}

Input

4
11 12 13 14

Output

52

value of s2=  11 12 13 14

Can you debug it yourself?

br.read() reads single character, so, for the above input, it reads 4. Now, you’re taking it into int. So, the ascii value of 4, i.e., 52 is stored in n. But the newline character is not yet read.
The next br.readline() will read until it encounters a '\n' character. This way, the s2 is NULL (in your terms).

ohh so the first br.readLine() starts reading from where br.read() left and keeps till it find line break and after this 2nd br.readLine() gets to read input provided.

But one thing which is still confusing is I haven’t provided any line breaks so where does this ’ \n ’ comes from which acts as line breaker for 1st input line ?

Kindly note that the input I provided in the comment has a newline character at the end of the first line.

ohh you mean when we hit enter after giving 1st input, this is how new line character is provided.

Thanks for explaining @suman_18733097

:slightly_smiling_face: