Monday, 18 May 2015

Java input from keyboard

Example: The following example demonstrates how to read from the keyboard. It reads two numbers from the keyboard and display their sum.

Doing input from keyboard is a bit complex in java. The program makes use of classes in java.io package. The program makes use of import statement to import the classes from the java.io package, which contains most of the classes related to Input/Output. You can compare import statement with the #include directive in C/C++ though the comparison is not exact. For example, to use input/output related functions in C/C++ you must include the compiler directive #include<stdio.h> in your program.

import java.io.*;

public class AddTwoNumbers {
public static void main(String args[]) throws IOException{
DataInputStream dataInputStream = new DataInputStream(System.in);
int n1 = 0, n2 = 0, sum = 0, len;
String number;
System.out.println("Enter first number: ");
number = dataInputStream.readLine();
n1 = Integer.parseInt(number);
System.out.println("Enter second number: ");
number = dataInputStream.readLine();
n2 = Integer.parseInt(number);
sum = n1 + n2;
System.out.println("Sum = "+ sum);
}

}

The first statement in the above program is an import statement, which imports all the classes in the package java.io so that the classes can be used without qualifying.

The program makes use of the class DataInputStream to read from the keyboard. Creating an object of this class by passing System.in as parameter to the constructor creates an input stream connected to keyboard/console. The readLine() instance method of the class DataInputStream can then be used to read one line at a time from the keyboard. The readLine() method returns the line read as a string, so it has to be converted into int before doing addition.

If you input a String containing alphabets, the program will terminate with the following run time exception:

java.lang.NumberFormatException: <<String containing alphabets>>

The above program will compile and run successfully but you will see the following warning message:

Note: AddTwoNumber.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details

The above message appears due to the use of instance method readLine() of the class DataInputStream. This method has some known bugs and therefore sun has marked this method as deprecated, which means that this method may be discontinued in the future version of Java and its use should be avoided as far as possible. A method is deprecated when It has some known bugs or has side effects, which can cause some problems or may affect the performance.

We can rewrite the above program by avoiding the use of DataInputStream class and using other classes, whose methods are not deprecated. The following example is rewritten for this purpose.

Example: 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AddTwoNumbers {
public static void main(String args[]) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n1 = 0, n2 = 0, sum = 0;
String number;
System.out.println("Enter first number: ");
number = bufferedReader.readLine();
n1 = Integer.parseInt(number);
System.out.println("Enter second number: ");
number = bufferedReader.readLine();
n2 = Integer.parseInt(number);
sum = n1 + n2;
System.out.println("Sum = "+ sum);
}
}


 

No comments:

Post a Comment