Keyboard Input BufferedReader Java


The previous program (KeyboardReading1.java) of reading keyboard input, using DataInputStream, raises a deprecation warning. To overcome this, in the following program, the readLine() method of BufferedReader is used.

Example on Keyboard Input BufferedReader Java

The following program takes input from keyboard to fill the array elements. The elements and their average are printed.

import java.io.*;
public class KeyboardReading2
{
  public static void main(String args[]) throws IOException
  {   
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    int marks[] = new int[3];    
                                            // populating array elements
    for(int i = 0; i < marks.length; i++)
    {
      System.out.println("Enter a whole number");
      String s1 = br.readLine();
      int x = Integer.parseInt(s1);
      marks[i] = x;
    }
                                         // printing the array elements
    int total = 0;
    System.out.print("The elements are: ");
    for(int i = 0; i < marks.length; i++)
    {
      System.out.print(marks[i]+"\t");
      total += marks[i];
    }
    System.out.print("\nThe average is : " + (total/marks.length));
  }
}
Importance of InpustStreamReader in Keyboard Input BufferedReader Java

Following statement raises compilation error.

BufferedReader br = new BufferedReader(System.in);

The System.in is a byte stream and cannot be chained to BufferedReader as BufferedReader is a character stream (this problem, we did not face with DataInputStream as DataInputStream and System.in are both byte streams, as in KeyboardReading1.java). The byte stream System.in is to be converted (wrapped) into a character stream and then passed to BufferedReader constructor. This is done by InputStreamReader. InputStreamReader, as the name indicates, is neither an input stream nor a reader. It is not a carrier of data. It is simply a wrapper around input stream to give a reader functionality. The InputStreamReader is used to link an input stream with character stream on reading-side. Similar stream, you get later on writing-side, OutputStreamWriter that links a FileInputStream with BufferedReader.

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

The above statement can be replaced with one statement using anonymous object of InputStreamReader as follows.

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

Note: This program does not raise deprecation warning while compilation as readLIne() method of BufferedReader is not deprecated.

int x = Integer.parseInt(s1);
marks[i] = x;

The data read with readLine() method is in the form of a string. It must be parsed as per the code requirement. The string s1 is parsed to an int value as the elements of the marks array are integers.

3 thoughts on “Keyboard Input BufferedReader Java”

  1. what to do if the user also wants to input the number of index ?

    import java.io.*;
    public class KeyboardReading2
    {
    public static void main(String args[]) throws IOException
    {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    int n = 0;

    int marks[] = new int[n];
    System.out.print(“Enter the number of Index: “);
    n = Integer.parseInt(br.readLine());
    // populating array elements
    for(int i = 0; i < marks.length; i++)
    {
    System.out.println("Enter a whole number");
    String s1 = br.readLine();
    int x = Integer.parseInt(s1);
    marks[i] = x;
    }
    // printing the array elements
    int total = 0;
    System.out.print("The elements are: ");
    for(int i = 0; i < marks.length; i++)
    {
    System.out.print(marks[i]+"\t");
    total += marks[i];
    }
    System.out.print("\nThe average is : " + (total/marks.length));
    }
    }

    it will only just tell
    The elements are: Exception in thread "main" java.lang.ArithmeticException: / by zero
    at KeyboardReading2.main(KeyboardReading2.java:30)

Leave a Comment

Your email address will not be published.