You are on page 1of 3

clsss DataInputStream

{
//data (variable)
DataInputStream //methods (functions)
}
============================

import java.io.DataInputStream;

 we access methods of a class we must first create class object (instance).


 Methods are access by use dot (.) operator

DataInputStream dis =new DataInputStream(System.in);

Here ,

 ‘dis’ is an object name create from DataInputStream class


 ‘new’ is a new key word for allocate memory for object
 ‘System.in’ is a reference of Keyboard device

readLine() This methods of DataInputStream class getting input from


----
keyboard until user press enter key. By default the data read from keyboard in
the form of String only. So by the help of wrapper classes conversion String
type into appropriate premitive type
Syntax :

============================

String var=dis.readLine();

Note : When using of readLine() method java generates run time error (Exception)

name is IOException. So we throwing the Excption.

So we import java.io.IOException class also.


Example program to accept person name and display name

import java.io.*;
class inputdemo
{
public static void main(String args[]) throws IOException
{
String myname;
DataInputStream dis = new DataInputStream (System.in);
System.out.println("Enter your name");
myname=dis.readLine();
System.out.println("Hello,"+myname+" Happy Independance Day");
}
}

Wrapper classes
convert String type into primitive type(int,float,double,boolean)

Data type Wrappr class


int Integer.parseInt(String)
float Float.parseFloat(String)
double Double.parseDouble(String)
boolean Boolean.parseBoolean(String)

A program demonstrate arithmetic operations on given two numbers. Accept numbers using
DataInputStream class

// A program to find arithmetic calculation on given two numbers


import java.io.*;
class arithdemo1
{
public static void main(String args[]) throws IOException
{
int a;
int b;
int add,sub,mul,rem;
float div;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter first number");
a=Integer.parseInt(dis.readLine());
System.out.println("Enter second number");
b=Integer.parseInt(dis.readLine());
add=a+b;
sub=a-b;
mul=a*b;
div=(float)a/b;
rem=a%b;

System.out.println("Addition is "+add);
System.out.println("Minus is "+sub);
System.out.println("Product is "+mul);
System.out.println("Division is "+div);
System.out.println("Remainder is "+rem);

}
}

You might also like