ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Code For BufferedReader Class Example in Java

Updated on October 31, 2013

Get A Website Plus a Free Domain Name in Just 1 Hour!

Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!

How to use BufferedReader?

One of the uses of BufferedReader in Java is to get the value of the variable input by the user, very much like what scanner does. The class BufferedReader should be also associated with an object. This object would be used to access the class BufferedReader. This is how we declare the bufferedReader with its own object:

BufferedReader putAnObjectHere = new BufferedReader(new InputStreamReader(;

After that, you can import the packages associated with the declaration above. There are actually 2 packages that you need to import. These packages are:



If you use netbeans, simply click the red error sign then click the first choice, do that twice. This is to automatically import the 2 packages above your main program and below the program’s package name.

Using the BufferedReader object on getting the integer input

To get the integer input by the user, simply declare a variable then access the BufferedReader class. The variable should be also declared in a String data type then parse it to integer in order to work. When I tried directly to declare an int, then print it on the screen, it doesn’t work correctly. See the example below on how to do it.

String inputBytheUser = input.readLine();

int parseInputBytheUser = Integer.parseInt(inputBytheUser);

Unlike the class Scanner, BufferedReader always need other packages to be imported before you can use it. In the first declaration above, you still need to use the IOException package in order for the code to work. This is how to import the exception:


If it is a little cloudy, you can refer to the code below for a better example.

Using the BufferedReader object on getting the String input

The highly use data types for beginners are always integer and string, so in this hub, we will also show you on how get the input of the user on the string data type. This is how we will do it:

String StringInput = input.readLine();

If you already import a package for IOException, no need to import for this, it will work

Sample Java Program for BufferedReader

package bufferedreader_in_java;


public class Main {

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

        BufferedReader input = new BufferedReader(new InputStreamReader(;       
        System.out.print("Input a String: ");
        String StringInput = input.readLine();

        System.out.print("Input an Integer: ");
        String integerInput = input.readLine();
        int ParseIntegerInput = Integer.parseInt(integerInput);


        System.out.println("The Integer Input: " + ParseIntegerInput);
        System.out.println("The String Input: " + StringInput);



    0 of 8192 characters used
    Post Comment

    No comments yet.