I am reading a number, then a string and then comparing the string. This works fine:
int num = sc.nextInt();
String S = sc.next();
if (S.equals("TEST")){
//do something
}
On giving i/p say 5 TEST, the program enters the if part.
But if I use .nextLine() instead, ie,
int num = sc.nextInt();
String S = sc.nextLine();
if (S.equals("TEST")){
//do something
}
It doesn't work. For the same input(5 TEST) it won't execute the if part.
I know .next() reads up till the first whitespace and .nextLine() reads up till it encounters "\n". Here both of them are returning the same string, so what's going wrong here?
EDIT: Found out that .nextLine() was reading the String alongwith the preceding whitespace. Why? Does the .nextInt() function return the cursor to the point before it encounters the delimiter?