0
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
       // sc.nextLine();
        int num=0,i=0,fnum=0;
        
       while(t-- >= 0){
        String str = sc.nextLine();    //10th line
        String sarr[] = str.split("");

        for(int j=0;j<str.length();j++){
            System.out.print(sarr[j]);
        }
        System.out.println();
       }
    }
}

Input: 
2
101
111111

Output:

101
111111

Why is the first line empty?(This was tested on a non interactive compiler) Also, i read that in java after taking integer input in Java , we have to use sc.nextLine(); before taking the immediate next String input.But when I use that in above program it gives me an error, why?

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at Main.main(Main.java:10)
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
  • The other error is caused because your loop is looping one iteration too much. It will try to fetch one more line where there is none left. Change `t-- >= 0` to `t-- > 0`. – Ivar Sep 24 '20 at 14:44
  • 1
    Ohh yes you are right. I didn't notice that.Thanks for the help @Ivar – Anubhav Mishra Sep 24 '20 at 14:53

0 Answers0