While running this code on netbeans it always run the if statement block but on compiling it on HackerRank code editor it always go into the else statement . Help me with the error.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
class Person {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
BufferedReader sb=new BufferedReader(new InputStreamReader(System.in));
int T=sc.nextInt();
for(int j=0;j<T;j++)
{String s="";
s=sb.readLine();
StringBuffer even=new StringBuffer();
StringBuffer odd=new StringBuffer();
if(s!=null)
{for(int i=0;i<s.length();i++)
{
if(i%2==0)
even.append(s.charAt(i));
else
odd.append(s.charAt(i));
}
System.out.println(even+" "+odd);
}
else
System.out.println("Enter some input");
}
}
}
Why are you using both scanner
and bufferedreader
? Try the next()
method from the scanner instead
s=sc.next();
If you want to make use of bufferedreader
then try these two statements instead.
int T=Integer.parseInt(sb.readLine());
for(int j=0;j<T;j++)
{
String s=sb.readLine();
Once if you make both the scanner
and bufferedreader
listen to input from the inputstream, How will the bufferedreader
know till where the input has been read? (But the scanner
knows it) That's why it returns null
instead. I hope you understood it.
To keep it simple, just use either the scanner
or the bufferedreader
not both.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.