Interesting Data input using Buffered Reader
Learn how to take input in Java using the BufferedReader class: reading strings, integers, decimals and characters, handling IOException, and how BufferedReader compares with the Scanner class, with solved programs for ICSE Class 9.
Taking input from the user is one of the first things you learn in ICSE Class 9 Computer Applications. Java offers more than one way to do it. This tutorial explains the BufferedReader method step by step and shows how it differs from the Scanner class.
Step 1: Import the java.io package
BufferedReader and InputStreamReader belong to the java.io package, so the program must start with:
import java.io.*;Step 2: Create the reader objects
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);System.in is the keyboard. InputStreamReader converts the bytes typed on the keyboard into characters, and BufferedReader collects those characters into a buffer so they can be read a whole line at a time. The two lines are often combined into one:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));Step 3: Read the data
The method readLine() always returns a String. To get a number, convert that String using a wrapper-class method:
String name = br.readLine(); // text
int age = Integer.parseInt(br.readLine()); // integer
double marks = Double.parseDouble(br.readLine()); // decimal number
char grade = br.readLine().charAt(0); // single characterStep 4: Handle IOException
readLine() can throw an IOException, which is a checked exception, so the method that uses it must declare it:
public static void main(String args[]) throws IOExceptionIf you forget throws IOException, the program will not compile.
Solved program: percentage calculator
import java.io.*;
class Percentage {
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.print("Enter marks in Computer (out of 100): ");
int comp = Integer.parseInt(br.readLine());
System.out.print("Enter marks in Maths (out of 100): ");
int maths = Integer.parseInt(br.readLine());
double percent = (comp + maths) / 2.0;
System.out.println(name + ", your percentage is " + percent + "%");
}
}Sample run: for Riya, 92 and 85, the output is Riya, your percentage is 88.5%. Notice 2.0 rather than 2, which avoids integer division.
BufferedReader vs Scanner
- Package: BufferedReader is in
java.io; Scanner is injava.util. - Reading numbers: BufferedReader reads a String that you convert with
Integer.parseInt()and similar methods; Scanner has ready-made methods such asnextInt()andnextDouble(). - Exceptions: BufferedReader needs
throws IOException; Scanner does not. - Speed: BufferedReader is faster for large amounts of input; Scanner is simpler to write.
Most ICSE schools now use the Scanner class, but questions on BufferedReader, readLine() and parseInt() still appear in Class 9 tests.
Practice questions
- Which package must be imported to use BufferedReader?
- What type of value does
readLine()return? - Write a statement to read a double value into the variable
priceusing BufferedReader. - Why must
main()declarethrows IOException?
Answers:
java.io- String
double price = Double.parseDouble(br.readLine());- Because
readLine()can throw IOException, which is a checked exception.
You can also watch Trushna Tejwani's video lesson on data input using BufferedReader on the Java by Trushna YouTube channel, then continue with our notes on Data input using the Scanner class.
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.