The diffrence in the code is in the deleteInput()-Method. Everything else is the same. The question is at the end, because it's about the console output.
The code:
public class DeletePreviousInputs {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws InterruptedException, IOException {
System.out.println("write something in the console");
/*
* What I wrote:
1 <enter> 2 <enter> 3 <enter> 4 <enter>
*/
Thread.currentThread();
Thread.sleep(8000); //simulates delay
deleteInput();
System.out.println("after input should be deleted");
System.out.println(scanner.nextLine());
System.out.println(scanner.nextLine());
System.out.println(scanner.nextLine());
}
//Version 1 of the deleteInput()-Method:
private static void deleteInput() throws InterruptedException {
try {
while (System.in.available() > 0) {
//This is the part, where the code will differ in Version 2
scanner.nextLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//Version 2 of the deleteInput()-Method:
private static void deleteInput() throws InterruptedException {
try {
while (System.in.available() > 0) {
//This is the part, where the code differs
Scanner dummyScanner = new Scanner(System.in);
dummyScanner.nextLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output version 1:
write something in the console
1
2
3
after input should be deleted
2
3
Output version 2:
write something in the console
1
2
3
after input should be deleted
Why does version 1 not delete/skip all of the previous input? Why does version 2 work perfectly?
I tried to delete all previous input of System.in so that only the latest/newest would be recognized by the Scanner. I didn't expect to see a diffrence in the functionality between the two deleteInput()-Methods. I do not understand why one version works and the other does not work.
Update:
@Sash (I can't answer to your comment for some reason, so I update here) I tried it and changed only the deleteInput()-method to this:
System.out.println("deleteInput()-method has started");
while (scanner.hasNextLine()) {
scanner.nextLine();
}
System.out.println("deleteInput()-method has ended");
But that creates a loop and the method is never left. Here is the output I get:
write something in the console
1
2
3
deleteInput()-method has started
If I replace scanner.nextLine() with:
if(scanner.nextLine().isEmpty()) {
break;
}
it also does not leave the method.
deleteInput
scanner.nextLine()
returns one line, but consumes the whole available inputSystem.out.println(scanner.nextLine())
has something to print,Additional note: System.in.available()
returns 0
after a single call to scanner.nextLine()
, because whole input is consumed by scanner
deleteInput
dummyScanner
consumes the whole available input.scanner.nextLine()
has nothing to readSystem.out.println(scanner.nextLine())
outputs nothingCode to illustrate this behavior:
System.out.println("Input 3 lines");
Thread.currentThread();
Thread.sleep(8000);
System.out.println("After sleep");
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
System.out.println("scanner1.nextLine() - " + scanner1.nextLine());
if (System.in.available() > 0) {
System.out.println("scanner2.nextLine() - " + scanner2.nextLine());
} else {
System.out.println("No input available");
}
System.out.println("scanner1.nextLine() - " + scanner1.nextLine());
System.out.println("scanner1.nextLine() - " + scanner1.nextLine());
On input:
1
2
3
Output is:
After sleep
scanner1.nextLine() - 1
No input available
scanner1.nextLine() - 2
scanner1.nextLine() - 3