Where are you using ctrl‑D? You do not usually use it when you are using a Scanner. I went through the API for
System.in, well more precisely an InputStream, and couldn't see anything about ctrl‑D or other file end characters. I shall try it out and report what happens.
I have had to edit your post because
the lines are too long, and noticed you used \n.
You should only ever use \n or \r if somebody has specified CR and LF characters. Since you were using printf, you should change the \n to %n