I've been racking my brain for a while trying to understand how Scanner works. So here's the code:
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String p = sc.nextLine();
System.out.println(s);
System.out.println(p);
System.out.println(sc.hasNextLine());
What I expect:
Love is good <- press ENTER
Love is blind <- press ENTER
Love is good <- output
Love is blind <- output
false
What I have:
Love is good <- I press ENTER
Love is blind <- I press ENTER
Love is good <- output
Love is blind <- output
<- press ENTER
true <- output
What I do not understand:
- Instead of immediate printing this line - System.out.println(sc.hasNextLine()); - it makes me to press ENTER again
- It prints true instead of false while there no more lines or symbols
What I have read: I have read a dozen of stackoverflow answers about using hasNextLine() after nextInt() and about how nextInt() does not consume the final symbol in the line but I don't understand why, even though I don't use nextInt() in here, I still need to press ENTER one more time and why hasNextLine() is true.