I'm solving a programming challenge on hacker-earth, in which I've to scan a large number of space separated String
s. To scan such a String I used nextLine()
method,about which I've read here. But It works only first time in the following while()
loop. and after that nothing is scanned or printed in output.Here is my code:
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-->0){
in.nextLine();
String s = in.nextLine();
String str[] = s.split(" ");
for(int i=0;i<str.length/2;i++){
String temp;
temp = str[i];
str[i] = str[str.length-i-1];
str[str.length-i-1] = temp;
}
for(int i=0;i<str.length;i++){
System.out.print(str[i]+" ");
}
}
I've Input like this:
2 //number of Strings to be Scanned.
Hello World
Hello StackOverFlow Users
For the first String Hello World
, output comes perfectly fine,but nothing happens after this output. What I'm doing wrong here? Any help will be appreciated.