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

User-Defined Methods in Java for ICSE Class 10: Parameters, Return and Overloading
ICSE Class 10 Computer Applications notes on user-defined methods: method prototype and signature, formal and actual parameters, return types, call by value vs call by reference, pure and impure methods, static methods and method overloading, with programs.

String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.

Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More
Every special number asked in ICSE Computer Applications – prime, perfect, Armstrong, palindrome, automorphic, neon, spy, Niven, duck, buzz, Disarium, special and pronic numbers – with definitions, examples and one complete Java program that checks them all.