Java Data Type How to - Handle continuous input by character








Question

We would like to know how to handle continuous input by character.

Answer

/*w w w . j  a  va 2s  . c  o m*/
import java.io.Console;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    Console cons = System.console();
    if (cons == null) {
      System.out.println("Console is null");
      System.exit(-1);
    } else {
      System.out.println("Enter text, or \"z\" to exit:");
      while (true) {
        char c = (char) cons.reader().read();
        System.out.println("" + c);
        if (c == 'z') {
          System.exit(0);
        }
      }
    }
  }
}