Home Java Java-get-example Get Character
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Get Character

Advertisement
In this example given below, we will learn how to read character from keyboard and display on standard output device. Method System.in.read() read character from keyboard and returns integer value of the character so we need to change type to char.

Get Character

     

In this example given below, we will learn how to read character from keyboard and display on standard output device. Method System.in.read() read character from keyboard and returns integer value of the character so we need to change type to char.

 

 

 

Here is the code of GetCharMethod

 

import java.io.*;
  public class GetCharMethod
  {
  public static void main(String args[]) throws IOException
  {
  char c; 
  System.out.println("Enter any character or Enter 
   the charater E to exit"
);
  while ( ( c = getChar() ) != 'E' )
  {
 System.out.println("You have entered " + c);
 }
  }
  static public char getChar() throws IOException
  {
  char ch = (char) System.in.read();
  input(); 
  return ch;
  }
  static public void input() throws IOException
  {
 while ( (char) System.in.read() != '\n' ); 
  }
  }

 

Output will be displayed as:

 

Download Source Code

 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2008

Ask Questions?    Discuss: Get Character   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
john gera
December 23, 2011
selenium

basics of selenium
DMCA.com