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
18,038 views

3 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.

answered Mar 4, 2019 by Priyaj
• 57,640 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
• 57,940 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 by Rohan
• 180 points

Related Questions In Java

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,890 points
180 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,170 points
3,780 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,170 points
1,638 views
0 votes
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,580 points
185 views
0 votes
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,270 points
188 views
0 votes
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
1,081 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,200 points
1,752 views
+1 vote
2 answers

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

Try this in case you don't know ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 57,940 points
20,923 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
• 57,940 points
736 views