before complaining about "repeated question" read the whole asking, this is different. It's not the same old question about why nextLine() after nextInt() doesn´t work.
I got this text on a file:
BEAR
PARROT
FROG
HORSE
When I copy the text on my clipboard I expect the nextLine()
to read my clipboard line by line.
- When I try it with an anonymous Scanner object I got this.
while (true) {
String animal = new Scanner(System.in).nextLine(); //I copy
//the whole text on my clipboard
System.out.println("animal = " + animal);
}
output:
BEAR
PARROT
FROG
HORSE
animal = BEAR
So it just read the first newline character, BEAR.
- So I tried with a local Scanner(System.in) variable.
Scanner input = new Scanner(System.in);
while (true) {
String animal = input.nextLine(); // I copy
//the whole text on my clipboard
System.out.println("animal = " + animal);
}
output:
BEAR
PARROT
FROG
HORSE
animal = BEAR
animal = PARROT
animal = FROG
animal = HORSE
It read all of the newline characters in the clipboard.
Can someone explain, what's going on? Maybe the question is more about the buffer than an anonymous intance of Scanner.
psdta: So sorry for so many edits, I really struggled formatting the text.