DYNAMIC INPUTS / INTERACTIVE INPUTS / RUNTIME INPUTS
- It is possible to give values to variables interactively through the keyboard at the time of program execution (Providing values at the time of runtime)
- Dynamic inputs are done by using stream concepts in java (how to get runtime input in java).
Streams
- In java, the input and output (I/O) is achieved with help of stream concept
- Stream is the sequence of bytes (objects)
- Streams are generally classified as two types like input stream, output stream
Input Streams
- If bytes flow from device to the main() function, then this process is called input
- Ex. Keyboard
Output Streams
- If bytes flow from main() to device, then this process is called output
- Ex. Display screen (Monitor)
Runtime Inputs
- Java provides several ways to perform dynamic inputs (runtime inputs) namely DataInputStream, BufferedReader, Scanner classes, etc, …
DATAINPUTSTREAM CLASS
- It is a predefined class and available in java.io.* package
- It is used to read java primitive data types (Ex. int, float, double, char, string, etc, …) from the input stream in a machine
- First we need to wrap an input stream (System.in) in a DataInputStream class and then read java primitive types from the DataInputStream. Because it reads data (numbers) instead of bytes
BUILT-IN INSTANCE METHODS OF DATAINPUTSTREAM CLASS
1. read()
- It is a built-in instance method of DataInputStream class
- It is used to read a single byte of data from keyboard
- Return type : int
2. readLine()
- Used to read line of text (string) from keyboard
- It is similar to gets() in c language
- It is similar to scanf() in c language and cin in c++ language
- Return type : String
3. readInt()
- Used to read an integer value
- Return type: int
4. readFloat()
- Used to read a float value
- Return type: float
5. readDouble()
- Used to read a double value
- Return type: double
6. readByte()
- Used to read a byte value
- Return type: byte
7. readBoolean()
- Used to read a boolean value
- Return type: int
8. readChar()
- Used to read a character value
- Return type: char
9. read(byte[] obj)
- It is used to read the number of bytes from the input stream and store them into byte array (byte [])
- Return type: int
EXAMPLE OF DYNAMIC INPUTS (RUNTIME INPUTS)
1. SOURCE CODE
(JRuntimeInputs.java)
import java.io.*;
public class JRuntimeInputs
{
public static void main(String[] args) throws Exception
{
// local variables declarations
String name,place;
int id;
// DataInputStream definition
DataInputStream ds=new DataInputStream(System.in);
System.out.println(“———————————“);
System.out.println(“\tRuntime (Dynamic) Inputs”);
System.out.println(“———————————“);
// read employee id
System.out.print(“Enter the Employee Id \t\t: “);
String sid=ds.readLine();
// convert string format number to integer number using parse method
id=Integer.parseInt(sid);
// read employee name
System.out.print(“Enter the Employee Name \t: “);
name=ds.readLine();
// read employee place
System.out.print(“Enter the Employee Place \t: “);
place=ds.readLine();
// print employee details
System.out.println(“———————————“);
System.out.println(“E-Id\t: “+id);
System.out.println(“E-Name\t: “+name);
System.out.println(“E-Place\t: “+place);
}
}
2. OUTPUT
MORE TUTORIALS