Read character from console using InputStream - Java Language Basics

Java examples for Language Basics:Console

Description

Read character from console using InputStream

Demo Code


import java.io.IOException;
 
public class Main {
 
        public static void main(String[] args) {
                 int iChar = 0;
                 //from w w w  . j  ava 2 s. c  o  m
                 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);
                 }
        }
}
 

Result


Related Tutorials