Keyboard Input DataInputStream


In keyboard reading, three programs are given using DataInputStream, BufferedReader and Scanner. Of all, using java.util.Scanner is the easier and includes many methods to check the input data is valid to read.

Example on Keyboard Input DataInputStream

Following program uses readLine() method of DataInputStream to read data from the keyboard.

import java.io.*;
public class KeyboardReading1
{
  public static void main(String args[]) throws IOException
  {
    DataInputStream dis = new DataInputStream(System.in);
    System.out.println("Enter your name: ");
    String str1 = dis.readLine();
    System.out.println("I know your name is " + str1);

    System.out.println("Enter a whole number: ");
    String str2 = dis.readLine();
    int x = Integer.parseInt(str2);

    System.out.println("Enter a double value: ");
    String str3 = dis.readLine();
    double y = Double.parseDouble(str3);

    if(x > y)
      System.out.println("First number " +x + " is greater than second number " + y);
    else
      System.out.println("First number " +x + " is less than second number " + y);

    dis.close();
  }
}

Keyboard Input DataInputStream
Output screenshot on Keyboard Input DataInputStream

The readLine() method of DataInputStream reads a line at a time and returns as a string, irrespective of what the line contains. Depending on the input value, the string is to be parsed into an int or double etc. This is extra work in the keyboard reading when compared to C/C++. In C/C++, parsing is done implicitly by %d and %f etc.

Note: This program compilation raises deprecation warning (observe the screen shot) as readLine() method of DataInputStream is deprecated, but the program works.

What is "System.in"?

" in" is an object of " InputStream" class defined in System class (like "out" is an object of PrintStream class defined in System class). It is declared as static and final object. The in object is connected implicitly to the " standard input stream" of the underlying OS.

DataInputStream dis = new DataInputStream(System.in);

System.in is a byte stream which is automatically connected to the keyboard reading mechanism of operating system. It is a low-level byte stream. The low-level byte stream is passed to high-level byte stream DataInputStream following the rules of chaining. It is chained to DataInputStream to facilitate the reading of user input with it 's readLine() method.

Like System.in is connected to the keyboard mechanism of OS, the System.out is connected to the standard output mechanism of OS.

12 thoughts on “Keyboard Input DataInputStream”

  1. I cant run this program please help me

    import java.io.*;
    import java.io.IOException;
    import java.io.DataInputStream;
    class cstd
    {
    int no, java, ca, ds, total;
    String name;
    float avg;
    cstd() throws IOException
    {
    DataInputStream d = new DataInputStream(System.in);
    System.out.println(“Enter Student Register Number:”);
    no = Integer.parseInt(d.readline());
    System.out.println(“Enter Student Name:”);
    name = d.readline();
    System.out.println(“Enter the Mark of Java:”);
    java = Integer.parseInt(d.readline());
    System.out.println(“Enter the Mark of CA:”);
    ca = Integer.parseInt(d.readline());
    System.out.println(“Enter the Mark of DS:”);
    ds = Integer.parseInt(d.readline());
    total = java+ca+ds;
    avg = total/3;
    }
    void display()
    {
    System.out.println(“*****************************:”);
    System.out.println(“Student Register Number:\t\t\t”+ no);
    System.out.println(“Student Name:\t\t\t” + name);
    System.out.println(“Mark of Java:\t\t\t”+ java);
    System.out.println(“Mark of CA:\t\t\t” + ca);
    System.out.println(“Mark of DS:\t\t\t” + ds);
    System.out.println(“*****************************:”);
    }

    }
    public class cstd
    {
    public static void main(String args[]) throws IOException
    {
    cstd C = new cstd();
    C.display();
    }
    }

  2. Thank you Sir A very good Explanation of Each and Every concept.But i don’t understand what is the relationship between System class and PrintStream class so that variable of System class ‘out’ of type PrintStream call method of PrintStream class means ‘println’.
    Sir, if it is Possible give an example where for access the data we use
    classname.classvarible.otherclassmethod();

    1. System.in is an InputStream connected implicitly to the in stream of OS. in stream of OS carries data from keyborad to CPU. So any data travelling between keyboard and CPU can be captured with System.in. This System.in stream is chained to DataInputStream to read the data from keyboard using the methods of DataInputStream like readLine() etc.

  3. very very useful site!!
    it will be just perfect if some possible vi-va questions are also added…
    thank you very much sir!!

Leave a Comment

Your email address will not be published.