You can simply reopen the Scanner the same way you did originally:
import java.util.*;
class Foo {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.err.println("Enter some text and hit some Ctr+D");
while (true) {
try {
String s = scanner.nextLine();
System.out.println("You wrote: " + s);
} catch(NoSuchElementException e) {
System.err.println("EOF. Retrying.");
scanner = new Scanner(System.in); // HERE
}
}
}
}
Note that this is an infinite loop if the stream flags EOF forever, such as when piping or redirecting a file, so it's your responsibility to verify that stdin is a tty and/or add a retry limit.