PhysicsTeacher.in

High School Physics + more

Java Program – how to use DataInputStream

Here is a Java program that helps you to understand how to use DataInputStream to capture user input.

Java Program – how to use DataInputStream to capture user input using readLine

import java.io.DataInputStream;
class Reading
{

public static void main(String args[])
{
DataInputStream din= new DataInputStream(System.in);
int i1=0;//initialization must to compile//error: variable i1 might not have been initialized
float f1=0.0f;//initialization must to compile//error: variable f1 might not have been initialized

try
{
System.out.println(“Enter an integer”);
i1=Integer.parseInt(din.readLine());
System.out.println(“Enter an float”);
f1=Float.valueOf(din.readLine()).floatValue();

}
catch(Exception e){}

System.out.println(“Integer Num:”+i1);
System.out.println(“Float Num:”+f1);

}

}

See also  Java programming language basics - pdf notes
Scroll to top
error: physicsTeacher.in