import java.util.Scanner;
class IDEONE{public static void main(String[] arg){
Scanner in =new Scanner(System.in);
int T=in.nextInt();
for(int i=0;i<T;i++){
String s =in.nextLine();
int l=s.length();
int flag=0;int count=0;if (s.charAt(0)=='0'){ count =1;}
char ch=s.charAt(0);
for(int j=1;j<(l);j++){ if(s.charAt(j)!=ch)flag++;ch=s.charAt(j); }
if((count==1 &&flag<3 &&flag >0||(count==0 && flag>-1 &&flag<2)))
{System.out.println("YES");}
else System.out.println("NO");
}}}
After taking input of T, create a dummy string and again take input. You must getting NZEC because s is capturing the '\n’character and is of length 0 in interation one.
Just add 2 lines after taking input of 2.
String s;
s=in.nextLine()
it is not working even after changes
Try this-
int T=in.nextInt();
String p;
p=in.nextLine();
import java.util.Scanner;
class IDEONE{public static void main(String[] arg){
Scanner in =new Scanner(System.in);
int T=in.nextInt(); String s;
in.nextLine(); //this line is added
for(int i=0;i<T;i++){
s =in.nextLine();
int l=s.length();
int flag=0;int count=0;if (s.charAt(0)==‘0’){ count =1;}
char ch=s.charAt(0);
for(int j=1;j<(l);j++){ if(s.charAt(j)!=ch)flag++;ch=s.charAt(j); }
if((count==1 &&flag<3 &&flag >0||(count==0 && flag>-1 &&flag<2)))
{System.out.println(“YES”);}
else System.out.println(“NO”);
}}}