1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* Read character from console using InputStream This example shows how to read a character from console window. This example also shows how to read user entered data from console window using System.in */ import java.io.IOException; public class ReadCharFromConsoleExample { public static void main(String[] args) { /* * To read a character from console use, * read method of InputStream class variable System.in * which defined as static variable. */ int iChar = 0; System.out.println("Read user input character example"); try { System.out.println("Enter a character to continue"); iChar = System.in.read(); System.out.println("Char entered was : " + (char)iChar); } catch(IOException e) { System.out.println("Error while reading : " + e); } } } /* Typical output would be Read user input character example Enter a character to continue a Char entered was : a */ |
Add Comment