How To Get Data From Keyboard In Java

In this article, we discuss how to get data from the keyboard in Java. Learn about InputStreamReader class, BufferedReader class, Scanner class, DataInputStream class.

Introduction

 
In this article, we discuss how to get data from the keyboard in Java.
 

Some ways to get data from the keyboard

 
The following are some ways to get data from the keyboard:
  1. Console (I/O) class
  2. BufferedReader (and InputStreamReader) class
  3. Scanner class
  4. DataInputStream class

1. Console (I/O) class

 
The Console (I/O) class reads from the keyboard.
 
Object
 
The System class provides a static method named "console" that returns the unique instance of the Console class.
 
Syntax
  1. public static Console console()  
  2. {  
  3.     -------  
  4.     -------  
  5. }  
Some common public method used in the Console class are:
  1. String readLine(): reads a single line of text from the console.
  2. String readLine(String formt, Object....args): reads the single line of text from the console.
  3. char() readPassword(): reads a password that is not displayed on the console.
  4. char[] readPassword(String formt, Object....args): reads a password that is not displayed on the console.
1.Examples
 
In this example, we make a class using console that reads the name of the user and prints it.
  1. import java.io.*;  
  2. class ConsoleEx {  
  3.     public static void main(String args[]) {  
  4.         Console con = System.console();  
  5.         System.out.println("Whats your name");  
  6.         String str = con.readLine();  
  7.         System.out.println("Welcome " + str + " to the java world");  
  8.     }  
  9. }  
Output
 
fig-3.jpg
 
2. Example
 
In this example, we obtain the instance of the Console class that reads a password but when we type the password it is shown blank but in the output it shows our password.
  1. import java.io.*;  
  2. class ConsoleEx2 {  
  3.     public static void main(String args[]) {  
  4.         Console con = System.console();  
  5.         System.out.println("Enter password-Any type whichever you want");  
  6.         char[] chr = con.readPassword();  
  7.         System.out.println("Now your password is");  
  8.         for (char ch1: chr)  
  9.             System.out.print(ch1);  
  10.     }  
  11. }  
Output
 
fig-4.jpg
 

2. InputStreamReader class

 
The InputStreamReader class is also used to read (or get) data from the keyboard. It mainly performs the following two tasks:
  • changes the byte-oriented stream into character-oriented stream.
  • provide a connection to input stream of keyboard.

BufferedReader class

 
The BufferedReader class can be used to read data line by line, using the readline() method.
 
1. Example
 
In this example, we are using both classes, used for getting data line by line.
  1. import java.io.*;  
  2. class InpReaderEx {  
  3.     public static void main(String[] args) throws Exception {  
  4.         InputStreamReader inpred = new InputStreamReader(System.in);  
  5.         BufferedReader bffr = new BufferedReader(inpred);  
  6.         System.out.println("Whats your name");  
  7.         String sname = bffr.readLine();  
  8.         System.out.println("welcome " + sname + " to the java world");  
  9.     }  
  10. }  
Output
 
fig-1.jpg
 
When we run our program, it asks for user input (whats your name). In this example, we provide "John" as input. Now the output is:
 
fig-2.jpg
 

3. Scanner class

 
We have seen that there are various ways to read input from the keyboard, java.Util.Scanner is one of them. This class broke the data into tokens using delimiter.
 
Some common methods are:
  • String next(): returns the next token from the scanner.
  • String nextLine(): moves the scanner position to the next line and returns the value as a string.
  • byte nextByte(): it scans the next token as a byte.
  • short nextShort();
  • int nextInt():
  • long nextLong():
  • float nextFloat();
  • double nextDouble();
Example
  1. import java.util.Scanner;  
  2. class ScannerEx {  
  3.     public static void main(String args[]) {  
  4.         Scanner scnr = new Scanner(System.in);  
  5.         System.out.println("Whats your rollno");  
  6.         int rllnum = scnr.nextInt();  
  7.         System.out.println("Whats your name");  
  8.         String sname = scnr.next();  
  9.         System.out.println("Your Rollno:" + rllnum + " name:" + sname);  
  10.     }  
  11. }  
Output
 
fig-5.jpg
 

4. DataInputStream class

 
The DataInputStream class reads primitive data types from an input stream. Applications use a data output stream to write data that can later be read by a data input stream.
 
A sample use of the constructor of the InputStream class is:
  1. InputStream in = DataInputStream (InputStream in);  
Some common public methods are:
  • final int read (byte[] s, int off, int len) throws IOException:
  • final int read (byte [] bt) throws IOException
  • to read the bytes:
    • final Boolean readBooolean() throws IOException,
    • final byte readByte() throws IOException,
    • final short readShort() throws IOException
    • final Int readInt() throws IOException
  • String readLine() throws IOException