Java I/O How to - Read user input char value from console via System.in.read()








Question

We would like to know how to read user input char value from console via System.in.read().

Answer

//from  ww w  . j av  a2 s.c o  m
import java.io.IOException;

public class Main {

  public static void main(String[] args) {

    try {
      char c = (char) System.in.read();
      while (c != '\r') {
        System.out.println(c);
        c = (char) System.in.read();
      }
    } catch (IOException e) {
    }

  }

}

The code above generates the following result.