Java Tutorial
BufferedReader(Reader inputReader)We can use read() method in BufferedReader to read a character. read method can throw IOException.
int read() throws IOExceptionReading Console Input Characters Example:
// Reading a character in java program import java.io.*; class ReadingConsoleInputTest { public static void main(String args[]) throws IOException{ char ch; //Creates Character input stream. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter characters, char 'x' to exit."); //read characters in console input do { ch = (char) br.read(); System.out.println(ch); } while(ch != 'x'); } }Output:
Enter characters, char 'x' to exit. a a b b c c d d x x
// Reading a string in java program import java.io.*; class ReadingStringTest { public static void main(String args[]) throws IOException{ String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter strings. 'exit' to exit."); //reading string in console input do { str = br.readLine(); System.out.println(str); } while(!str.equals("exit")); } }Output:
Enter strings. 'exit' to exit. welcome to java! welcome to java! Reading console input string in java program Reading console input string in java program exit exit
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page