Why usage of nextLine() is not efficiently takes the input? If i use next() instead of nextLine() the code is super good but why? whats the reason behind it.Where and all i should use next and nextLine();
import java.util.Scanner;
public class Sample{
public static void main(String args[]) {
String a,b;
Scanner scan = new Scanner(System.in);
q = scan.nextInt();
for(int m=0;m<q;m++) {
a = scan.nextLine();
b = scan.nextLine();
System.out.println(a + "" + b);
}
}
}
Expected:
2
mnopm
nop
mnop nop
abcdee
bee
abcdee bee
Actual:
2
mnop
mnop(output appears before taking the 2nd input)
abcee
bee
abcee bee