How to take input using BufferedReader in Java

+1 vote
Can someone explain me the method of taking input by using BufferedReader in Java?
Mar 4, 2019 in Java by Shruti
123,737 views

5 answers to this question.

0 votes

BufferedReader is used to decrease the time for taking input. Generally, we use the Scanner class.

BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
int T= Integer.parseInt(inp.readLine()); // for taking a number as an input 
String str = inp.readLine(); // for taking a string as an input

For the case of an integer, you can take only input per line.

If you are not the Java expert, then its suggested to enroll now with Java certification course and become the certified Java expert.

answered Mar 5, 2019 by Priyaj
• 58,020 points
+1 vote

Reading User's Input using BufferedReader class:

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. Here’s an example:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = reader.readLine();
System.out.println("Your name is: " + name);

In the above example, the readLine() method reads a line of text from the command line.

answered Aug 16, 2019 by Sirajul
• 59,230 points
+1 vote
yupp i am explaining every thing ..

We take Buffered Reader because previously in " cmd line argument " have some drawback i.e

1. at run end user didn't know which type of value,what value,in which order we have to take value like this not get any information of input value at runtime ...so..we" BufferReader" introduce it is a class which is introduced by" SUN MICROSYSTEM"

now , so when you take" BufferReader "then it will show all details at runtime which is demerit in "cmd line argument".

and when you enter detail at cmd prompt after asking value then it will store in "BufferReader Object"

and after storing it will return value as string format which is also in cmd and we use wrapper class so that what Primitive data type you want you will get it.

and for reading given input we take readline()....now what its mean firstly only read() will take first byte of typed input and store so readline() will read complete line from starting to the time of pressing enter button so,,

its store in BufferReader object its that we have to create one object for storing

BufferReader br=new BufferReader(new InputStrearmReader(System.im));

br object variable will creates a variable space with default value.

and in pakage IO its available so InputStreamReader taken and it will store input value which is type by keyboard and pass to BufferReader object.

and "System.in" is taken because it will connection to keyboard so we passed.

okkk now , let we have to take a num from keyboard and show as output by using BufferReader nw see below.

object creation =  BufferReader br=new BufferReader(new InputStrearmReader(System.im));

                             System.out.println("Enter Number");

                             int num = Integer.parseInt(br.readline();

now see algorithm how its working in background in simple layman method...

when you type lets 20  which taken inside is in string form "20" and when you press enter then the string value will pass through"system.in " type tunnel which is connected to  keyboard to BR(bufferreader ) object  and when it reach in BR Object then stores there and after that through br.readline(); it will read first store value and return from there in string form "20" and then "Integer.parseInt" convert it into Integer type and give output as 20 which you read on cmd output through Sopln(num).

I hove you understand thanks....for further help ask again and again

from Rohan
answered Feb 1, 2020 by Rohan
• 180 points
0 votes

This is the Java classical method to take input, Introduced in JDK1.0. 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.

  • The wrapping code is hard to remember.

Program:

filter_none

edit

play_arrow

brightness_4

// Java program to demonstrate BufferedReader

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Test 

{

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

    {

        //Enter data using BufferReader

        BufferedReader reader = 

                   new BufferedReader(new InputStreamReader(System.in));

         

        // Reading data using readLine

        String name = reader.readLine();

  

        // Printing the read line

        System.out.println(name);        

    }

}

Input:

Edureka

Output:

Edureka
answered Dec 14, 2020 by Gitika
• 65,770 points
0 votes

The BufferedReader class of Java is used to read the stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object as a parameter.

This class provides a method known as readLine() which reads and returns the next line from the source and returns it in String format.

The BufferedReader class doesn’t provide any direct method to read an integer from the user you need to rely on the readLine() method to read integers too. i.e. Initially you need to read the integers in string format.

The parseInt() method of the Integer class accepts a String value, parses it as a signed decimal integer and returns it.

Using this convert the read Sting value into integer and use. In short, to read integer value-form user using BufferedReader class −

  • Instantiate an InputStreamReader class bypassing your InputStream object as a parameter.

  • Then, create a BufferedReader, bypassing the above obtained InputStreamReader object as a parameter.

  • Now, read integer value from the current reader as String using the readLine() method.

  • Then parse the read String into an integer using the parseInt() method of the Integer class.

Example

The following Java program demonstrates how to read integer data from the user using the BufferedReader class.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Employee{
   String name;
   int id;
   int age;
   Employee(String name, int age, int id){
      this.name = name;
      this.age = age;
      this.id = id;
   }
   public void displayDetails(){
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Id: "+this.id);
   }
}
public class ReadData {
   public static void main(String args[]) throws IOException {
      BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter your name: ");
      String name = reader.readLine();
      System.out.println("Enter your age: ");
      int age = Integer.parseInt(reader.readLine());
      System.out.println("Enter your Id: ");
      int id = Integer.parseInt(reader.readLine());
      Employee std = new Employee(name, age, id);
      std.displayDetails();
   }
}

Output

Enter your name:
Krishna
Enter your age:
25
Enter your Id:
1233
Name: Krishna
Age: 25
Id: 1233
answered Dec 14, 2020 by Rajiv
• 8,870 points

Related Questions In Java

0 votes
1 answer

How to take character input in Java?

In Java, you can take character input ...READ MORE

answered Oct 19, 2023 in Java by anonymous
• 3,360 points

edited Oct 19, 2023 by anonymous 705 views
0 votes
1 answer

How to encode data using Base64 in Java?

import org.apache.commons.codec.binary.Base64; We can not use sun.* packages ...READ MORE

answered May 30, 2018 in Java by Sushmita
• 6,920 points
1,066 views
0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,190 points
5,343 views
0 votes
1 answer

How to read an XML file using XPath in Java?

Import the packages required to work with ...READ MORE

answered Jun 14, 2018 in Java by Akrati
• 3,190 points
4,267 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,330 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,188 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,748 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,586 views
+1 vote
4 answers

How to take multiple integer input in one line using BufferedReader in Java?

To do this, we could read in ...READ MORE

answered Dec 14, 2020 in Java by Roshni
• 10,480 points
98,104 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
2,401 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP