TutorialsICSEClass 9Computer Applications

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.

Updated 8 min read
Tutorials

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 character

Step 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 IOException

If 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 in java.util.
  • Reading numbers: BufferedReader reads a String that you convert with Integer.parseInt() and similar methods; Scanner has ready-made methods such as nextInt() and nextDouble().
  • 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

  1. Which package must be imported to use BufferedReader?
  2. What type of value does readLine() return?
  3. Write a statement to read a double value into the variable price using BufferedReader.
  4. Why must main() declare throws IOException?

Answers:

  1. java.io
  2. String
  3. double price = Double.parseDouble(br.readLine());
  4. 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.