Skill Programming EXPT 2
Skill Programming EXPT 2
2
Aim: To Write Java Program on accepting input through keyboard.
Objectives:-Student/we will learn the standard IO classes and the method from that class to
read the input accepted from user through keyboard.
Theory:
As we are studying the methods of accepting the input from user, we will see
DataInputStream , BufferedReader and Scanner class in detail here .
2) DataInputStream class : A data input stream enables an application to read primitive Java
data types from an underlying input stream in a machine-independent way(instead of raw bytes).
That is why it is called DataInputStream – because it reads data (numbers) instead of just bytes.
An application uses a data output stream to write data that can later be read by a data input
stream.
3) , BufferedReader class
Java BufferedReader class is used to read the text from a character-based input stream. It can be
used to read data line by line by readLine() method. It makes the performance fast. It
inherits Reader class.
4) Scanner class
Scanner is a class in java.util package used for obtaining the input of the primitive types like int,
double, etc. and strings. It is the easiest way to read input in a Java program, though not very
efficient if you want an input method for scenarios where time is a constraint like in competitive
programming.
Step 1: Start
Step 3: In class maxim, Declare n initialize input variables x,y,z,w of type int and max of
type int use Command line argument to give input value to x, for y, use readline() method
of DataInputStream classto read the input value accepted through key board. for z, use
readline() method of BufferedReader class to read the input value accepted through key
board. for w, use nextInt() method of scanner class to read the input value accepted through
key board.
Step 4: Assume x is maximum and comparing x with remaining values, Based on the status
of the conditions, find out the maximum value.
Step 6: stop
Program(Code):
import java.io.*;
import java.util.*;
class maxim
{
public static void main(String args[])throws Exception
{
int x,y=0,z=0,w=0,max;
DataInputStream d1 =new DataInputStream(System.in);
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
x=Integer.parseInt(args[0]);
try
{
System.out.println("Enter value of y");
y=Integer.parseInt(d1.readLine());
}
}
Output:
C:\>d:
D:\>cd programs
D:\programs>path="C:\jdk1.6.0_23\bin";
D:\programs>javac maxim.java
Note: maxim.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
D:\programs>java maxim 7
Enter value of y
23
Enter value of z
41
Enter value of w
4
maximum=41
Outcome: With this we/student learned to use method like readLine(() method of
DataInputStream class , BufferedReader class and nextInt() of scanner class for reading
input accepted through keyboard in java program.