---
title: "How Many Ways To Reading Data From Keyboard in Java"  
description: "In Java, Their are several ways to taken an input from the keyboard.such like InputStreamReader Class,Scanner class,Console class etc."  
author: "Anupam Mishra"  
published: 2015-12-01  
updated: 2020-02-02  
canonical: https://www.mindstick.com/articles/11929/how-many-ways-to-reading-data-from-keyboard-in-java  
category: "java"  
tags: ["java", "j2ee"]  
reading_time: 3 minutes  

---

# How Many Ways To Reading Data From Keyboard in Java

##### There are many ways to read data from the keyboard in java:

##### 1. InputStreamReader Class

##### 2. Scanner Class

##### 3. Using Command Line Arguments

##### 4. Console Class

##### 1. InputStreamReader Class:

[InputStreamReader](https://www.mindstick.com/forum/161307/what-is-the-role-of-inputstreamreader-in-java-input-handling) class can be used to read data from keyboard for two reasons:

I. Connect to input stream of keyboard

II. It’s helpful for converting byte-oriented stream to [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character)-oriented stream.

BufferedReader class can be used to read data [line by line](https://www.mindstick.com/interview/34101/how-do-you-read-a-file-line-by-line-using-a-generator-like-approach-e-g-yield-return) using its readLine () method.

For example, we have want to take an input from the keyboard using InputStreamReader class.

```
import java.io.*;

public class InputByBufferedReader

{

public static void main(String[] args)throws IOException

{

InputStreamReader isr= new InputStreamReader(System.in);

BufferedReader br= new BufferedReader(isr);

System.out.println("Enter your nic name: ");

String name = br.readLine();

System.out.println("Enter your Address : ");

String add = br.readLine();

System.out.println("your Nic Name : "+name);

System.out.println("your Address is : "+add);

}}
```

Output:

Enter your nic name: xxx

Enter your Address : YYY

your nic name: xxx

your Address is :YYY

\

##### 2. Scanner Class:

The java.util.[Scanner class](https://www.mindstick.com/forum/1579/how-to-use-the-scanner-class-in-java) breaks the input into tokens using delimiter (i.e. Whitespace (By Default)). Its provide various methods to read and parse the various primitive values.\
Scanner class is used to parse text for string and primitive types using [regular expressions](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table).\
For example, we have taken a string. Java Scanner class which reads the int, [string value](https://www.mindstick.com/forum/23050/how-to-convert-string-value-to-hexa-value-in-vb-dot-net) as an input from that string.

> ```
> import java.util.Scanner;public class
> InputByScannerDelimeter{public static void main(String[]
> args){try{String Query= "Hi I am only
> 24";//
> The \s represents whitespace.Scanner sc= new
> Scanner(Query).useDelimiter("\\s");System.out.println(sc.next());System.out.println(sc.next());System.out.println(sc.next());System.out.println(sc.next());System.out.println(sc.nextInt());}catch(Exception
> e){System.out.println(e);}}}
> ```
>
> ##### Output:
>
> \
>
> Hi
>
> I
>
> Am
>
> Only
>
> 24
>
> \

##### 3. Using Command Line Arguments:

In java, Command [Line Arguments](https://www.mindstick.com/forum/10/is-there-a-method-to-get-command-line-arguments-in-vb-dot-net) is a one of the way to passed arguments at the time of running the java program.The arguments can be passed from the console can be received in the java program and it can be used as input. We have pass N numbers of arguments from the [command prompt](https://answers.mindstick.com/qa/51716/how-to-make-bootable-pendrive-using-cmd-windows-command-prompt).

For example, We have taken input from the console at the time of running the java program and sum of these integer input.

> ```
> public class InputUsingCMD{public static void main(String[] args){int sum=0;for(int i=0;i<args.length;i++){sum+=Integer.parseInt(args[i]);}System.out.println("The sum of the arguments is
> "+sum);}}
> ```
>
> ## Output:
>
> Java InputUsingCMD 1 2 3 5
>
> The sum of the arguments is 11

##### 4. Console Class

The java console class is be used to get [input from console](https://www.mindstick.com/forum/179/problem-in-taken-input-from-console). It provides methods to read text and password.

If you read password using Console class, it will not be displayed to the user.

The java.io.Console class is attached with system console internally.

For example ,To read text from console.

> ```
> import java.io.*;  class InputByConsole{
> public static void main(String
> args[]){ Console
> con=System.console();  System.out.println("Enter
> your name: ");  String n=con.readLine();System.out.println("Welcome
> "+n);  }}
> ```
>
> ## Output:
>
> Enter your name: **Anupam Mishra**
>
> Welcome Anupam Mishra

---

Original Source: https://www.mindstick.com/articles/11929/how-many-ways-to-reading-data-from-keyboard-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
