# Taking Input from Users in Java | Scanner, BufferedReader, InputStream

- Link: https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/
- Published: 2018-11-03T20:04:29+05:30
- Author: Ashishkumar Vishwakarma

In the following program, we will see data on how to take input from Users in Java
using the following methods:

 1. BufferedReader
 2. Reading input data using Data InputStreamReader
 3. Reading input data using Scanner

![Taking Input from Users in Java | Scanner, BufferedReader, InputStream ](https://
technotaught.com/wp-content/uploads/2020/11/Taking-Input-from-Users-in-Java.png)

Also, you can use the Java [compiler](https://www.onlinegdb.com/online_java_compiler)
to compile a program.

## 1. Buffered Reader class.

This is the Java classical method to take input. This method is used by wrapping
the System.in (standard input stream) in an InputStreamReader which is wrapped in
a BufferedReader, we can read input from the user in the command line.

**Advantages:**

 * The input is buffered for efficient reading.

**Drawback:**

 * The wrapping code is hard to remember.

### Java BufferedReader class Example

Roll number, name, salary take Input from Users.

##### SOURCE CODE:

    ```language-markup-templating
    import java.io.*;
    class Rbuf
    {
        public static void main(String args[]) throws IOException
     {
        int rollno;
        String name;
        float salary;

        BufferedReader s=new BufferedReader(new 
         InputStreamReader(System.in)); 
        //Create  object  

        System.out.println("Enter the Roll no:");
        rollno=Integer.parseInt(s.readLine()); //Input

        System.out.println("Enter the Name:");
        name=s.readLine(); //Input

        System.out.println("Enter the Salary:");
        salary=Float.parseFloat(s.readLine()); //Input

        System.out.println("Roll no:"+rollno);
        System.out.println("Name:"+name);
        System.out.println("Salary:"+salary);
     } //End of main
    } //End of classimport java.io.*;
    ```

##### **OUTPUT:**⏎ C:\A>javac Rbuf.java

> C:\A>java Rbuf
> Enter the Roll no:
> 59
> Enter the Name:
> SAM
> Enter the Salary:
> 500
> Roll no:59
> Name:SAM
> Salary:500.0

## 2. Data input Scream Class

Java DataInputStream class allows an application to read primitive data from the
input stream in a machine-independent way. Java application generally uses the data
output stream to write data that can later be read by a data input stream.

### Data input Scream Class Example

Take roll number, name, salary from user as an input.

##### SOURCE CODE:

    ```language-markup-templating
    import java.io.*;
    class Rdiss
    {
        public static void main(String args[]) throws IOException
     {
        int rollno;
        String name;
        float salary;
        DataInputStream s=new DataInputStream(System.in); //Create 
        object

        System.out.println("Enter the Roll no:");
        rollno=Integer.parseInt(s.readLine()); //Input

        System.out.println("Enter the Name:");
        name=s.readLine(); //Input

        System.out.println("Enter the Salary:");
        salary=Float.parseFloat(s.readLine()); //Input

        System.out.println("Roll no:"+rollno);
        System.out.println("Name:"+name);
        System.out.println("Salary:"+salary);
     } //End of main
    } //End of class
    ```

**OUTPUT:﻿**

> C:\A>javac Rdiss.java
> C:\A>java Rdiss
> Enter the Roll no:
> 01
> Enter the Name:
> SAM
> Enter the Salary:500
> Roll no:1
> Name:SAM
> Salary:500.0

## 3. Scanner class

The `Scanner` class is used to get user input, and it is found in the `java.util`
package.

To use the `Scanner` class, create an object of the class and use any of the available
methods found in the scanner class documentation. In our example, we will use the`
nextLine()` method, which is used to read strings

### Scanner class Class Example

Take roll number, name, salary from user as an input.

##### **Source Code**

    ```language-markup-templating
    import java.util.*;
    class Rscanner
    {
        public static void main(String args[]) 

     {
       int rollno;
       String name;
       float salary;
       Scanner s=new Scanner(System.in); //Create object

        System.out.println("Enter the Roll no:");
        rollno=s.nextInt(); //Input

        System.out.println("Enter the Name:");
         name=s.next(); //Input

        System.out.println("Enter the Salary:");
        salary=s.nextFloat(); //Input

        System.out.println("Roll no:"+rollno);
        System.out.println("Name:"+name);
        System.out.println("Salary:"+salary);
     } //END OF MAIN
    } // END OF CLASS
    ```

#### **OUTPUT.**

> C:\A>javac Rscanner.java
> C:\A>java Rscanner
> Enter the Roll no:
> 01
> Enter the Name:
> SAM
> Enter the Salary:
> 500
> Roll no:1
> Name:SAM
> Salary:500.0

Also, View:[ – Write a program to create a registration form using AWT](https://technotaught.com/registration-form/).

Also, View- [Write a java program to create a window using swing.](https://technotaught.com/22-write-a-java-program-to-create-a-window-using-swing/)

### Share this:

 * [  X ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?share=twitter)
 * [  Facebook ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?share=facebook)
 * [More](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/#)

 * [  WhatsApp ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?share=linkedin)
 * [  Email ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?subject=%5BShared%20Post%5D%20Taking%20Input%20from%20Users%20in%20Java%20%7C%20Scanner%2C%20BufferedReader%2C%20InputStream&body=https%3A%2F%2Ftechnotaught.com%2Ftaking-input-from-users-in-java-scanner-bufferedreader-inputstream%2F&share=email)
 * [  Reddit ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?share=reddit)
 * [  Tumblr ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?share=pinterest)
 * [  Telegram ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/?share=telegram)
 * [  Print ](https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/#print?share=print)

### _Related_

## Comments

### By: Steph B

- Link: https://technotaught.com/taking-input-from-users-in-java-scanner-bufferedreader-inputstream/#comment-134
- Published: 2020-04-23T07:54:45+05:30

This Post is very cool and more informative to me. Thanks for posting it.
