Accept Input Using BufferedReader Class in Java

// Java program to Accept input Using Buffered Reader Class in Java

import java.io.*;
class AcceptInput
{
public static void main(String[] args) throws IOException
{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String name = reader.readLine();
        System.out.println("Hello : "+name);
}
}

 

The BufferedReader class in Java is used to read input from the user efficiently. It is commonly used for handling character input from the keyboard and reading text data quickly.

The BufferedReader class belongs to the java.io package and is often considered faster than some other input methods because it reads multiple characters at once.

This article explains how to accept input using the BufferedReader class in Java with syntax, examples, output, and explanation.

What is BufferedReader in Java?

BufferedReader is a predefined Java class used to read text input from the keyboard or files.

It reads data efficiently by buffering characters, arrays, and lines.

The BufferedReader class is mainly used when fast input performance is required.

Required Packages

Before using BufferedReader, the following packages must be imported.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

Syntax of BufferedReader

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

Explanation

  • BufferedReader → Reads text efficiently
  • InputStreamReader → Converts byte stream into character stream
  • System.in → Accepts keyboard input

Java Program to Accept String Input

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class BufferedReaderExample {

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

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

        System.out.print("Enter your name: ");

        String name = br.readLine();

        System.out.println("Your name is: " + name);
    }
}

Output

Enter your name: Rahul
Your name is: Rahul

Java Program to Accept Integer Input

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class IntegerInputExample {

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

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

        System.out.print("Enter a number: ");

        int number = Integer.parseInt(br.readLine());

        System.out.println("Entered number is: " + number);
    }
}

Output

Enter a number: 50
Entered number is: 50

How BufferedReader Works

The BufferedReader class reads text input from the keyboard using System.in.

The readLine() method reads the complete line entered by the user as a string.

For numeric input, conversion methods like Integer.parseInt() are used to convert strings into integer values.

Advantages of BufferedReader

  • Faster input performance
  • Efficient for reading large text input
  • Reads complete lines easily
  • Useful in competitive programming and large applications

Disadvantages of BufferedReader

  • More complex than Scanner class
  • Requires exception handling
  • Input conversion needed for numeric values
  • Less beginner-friendly compared to Scanner

Difference Between BufferedReader and Scanner

FeatureBufferedReaderScanner
SpeedFasterSlower
Packagejava.iojava.util
Exception HandlingRequiredUsually Not Required
Numeric ParsingManualAutomatic
Beginner FriendlyLessMore

Important Note About BufferedReader

The BufferedReader class is useful for fast input operations in Java. However, beginners often prefer the Scanner class because it is easier to understand and requires less manual conversion.

In competitive programming and performance-based applications, BufferedReader is commonly preferred because of its speed.

Common Methods of BufferedReader

MethodDescription
readLine()Reads a complete line
close()Closes the stream
ready()Checks whether stream is ready

Frequently Asked Questions

What is BufferedReader in Java?

BufferedReader is a Java class used to read text input efficiently from the keyboard or files.

Which package contains BufferedReader?

The BufferedReader class belongs to the java.io package.

Why is BufferedReader faster?

It reads multiple characters at once using buffering, which improves input performance.

What is readLine() in Java?

The readLine() method reads an entire line of text entered by the user.

Which is better: Scanner or BufferedReader?

Scanner is easier for beginners, while BufferedReader is generally faster and preferred for high-performance input handling.

Conclusion

The BufferedReader class in Java is an efficient way to accept input from the user. It is widely used in Java applications where faster input handling is required.

By learning BufferedReader, students can understand Java input streams, exception handling, and efficient text processing techniques.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top