A Class Whose readChar() Method Reads One Character from the Standard Input - Java Language Basics

Java examples for Language Basics:int

Description

A Class Whose readChar() Method Reads One Character from the Standard Input

Demo Code

import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    System.out.print("Enter some text and press Enter key: ");
    char c = readChar();
    System.out.println("First character you entered is: " + c);
  }/*from ww  w.  j  a  v  a  2 s. co m*/
  public static char readChar() {
    char c = '\u0000';
    int input = 0;
    try {
      input = System.in.read();
      if (input != -1) {
        c = (char)input;
      }
    }
    catch (IOException e) {
      System.out.print("IOException occurred while reading input.");
    }    
    return c;
  }  
}

Result


Related Tutorials