0

I want to receive input from the keyboard one character at a time. The input can include white space, be of any length, and doesn't require a special character to mark its end.

The problem is that if I use an InputStreamReader, it doesn't know when the end of the stream is reached because there is no limited buffer size and no special character to let it know when the end of input is reached.

I thought I could get it to work when I discovered that InputStreamReader.read() == -1 means that the end of input has been reached. However, putting an if statement to check if InputStreamReader.read() == -1 doesn't work because it never reaches this case.

InputStreamReader userInput = new InputStreamReader(System.in);
boolean end = false;

while(! end) {
    int c = userInput.read();
    if(c == -1) {
    end = true;
    } else {
    System.out.println(c);
    }
}

With the above code, I'd expect that after the user enters their input into the console, it prints each character's ASCII code one by one and then terminates.

However, the program continues to run and receive input because the if-condition is never met.

How can I let the program know when the end of input is reached?

EDIT: I discovered that the requirements of my assignment allow me to use the EOF character to mark the end of input, however when I enter it into the console without first pressing Enter, the program terminates without reading any of the preceding input. I'm using IntelliJ as my IDE.

jipthechip
  • 157
  • 1
  • 13
  • do you need to read the characters one by one or just print it one by one? – Hugo Sartori Jan 24 '19 at 03:21
  • "End of input" means an end-of-file condition, ctrl/d on Unix/Linux systems, not that the user has taken his fingers off the keyboard. –  Jan 24 '19 at 03:22
  • what is your definition of end of input ? – mkjh Jan 24 '19 at 03:48
  • I don' think this can work with a terminal (command line) input. Java just doesn't support it. You might be able to show a GUI and use a KeyListener interface: https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html – markspace Jan 24 '19 at 05:29
  • "character's ASCII code": Um, actually, "[An InputStreamReader is a bridge from byte streams to _character_ streams: It reads bytes and decodes them into characters](https://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html)," where a "character" is the sentinel -1 or a `char` value, where "[a `char` value…represents a code unit of the UTF-16 encoding](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html)" of the [Unicode](http://www.unicode.org/charts/nameslist/index.html) character set. – Tom Blodget Jan 24 '19 at 08:31
  • @another-dave I discovered that I'm allowed to use the EOF character, but with the above code I need to press enter before typing it in or all the input will be ignored. Any idea how to fix it? – jipthechip Jan 24 '19 at 16:46
  • #EOF: https://stackoverflow.com/a/11968742/592355 – xerx593 Jan 24 '19 at 18:17

0 Answers0