Search code examples
javafile-iostdinroman-numerals

Java -- Read from std input, one char at a time


I'm having trouble determining the best way to read some input in for a java program. It needs to read in the individual characters of a roman numeral, and then perform some operation on it.

There are, however, several catches. The input must be read from the standard input, because input redirection is being used.

Additionally, I need to be able to detect both the pair of CR/LF characters to determine an end of line, and the EOF to determine the end of the file.

What's the best way to accomplish this? I've snooped around, and I found out that Scanner doesn't have a .nextChar class (which would have worked perfectly).


Solution

  • You can use the read method of the FileReader class (which it inherits from the InputStreamReader class). This will read one character at a time and return a -1 when the end of the file/stream is reached.