Neither approach is correct. The correct way to read characters from System.in
is to use an InputStreamReader
(or a Scanner
if that provides the right functionality). The reason is that InputStream.read()
reads a single byte, not characters, and some characters require reading more than one byte. You can also specify the character encoding to be used when converting bytes to characters.
Reader rdr = new InputStreamReader(System.in);
int i = rdr.next();
if (i == -1) {
// end of input
} else {
// normal processing; safe to cast i to char if convenient
}