Input in Java with
InputStreamReader + BufferedReader.
▪ Provides a user-friendly environment for input operations.
▪ Allows users to change data during program execution.
▪ Uses a combination of two classes:
▪ InputStreamReader
▪ BufferedReader
Role of InputStreamReader
▪ Converts data from the keyboard into machine code (binary form).
▪ Transfers binary input to CPU as a streamlined bit pattern.
▪ Ensures input bits are not disturbed, preventing input errors.
▪ Handles the conversion from byte stream to character stream.
InputStreamReader in = new InputStreamReader(System.in);
This statement creates an object of InputStreamReader type to accumulate streamlined bit
pattern from the input device.
Role of BufferedReader
Uses a buffer device (temporary memory) between input unit and processor.
Helps synchronize speed between I/O devices and CPU.
Stores streamlined input so data can be supplied to the CPU without delay.
Efficiently reads character-based input from console.
BufferedReader in = new BufferedReader(in);
This statement is used to create an object 'in' of
BufferedReader type to store the input data entered
from the keyboard.
Step-by-Step Setup
Import Required Package import java.io.*;
Define a Class public class MyInputProgram
Main Method Declaration public static void main(String args[]) throws IOException
InputStreamReader in = new InputStreamReader(System.in);.
BufferedReader in = new BufferedReader(in);.
public static void main(String args[]) throws IOException
Keywords Explanation
public: Accessible from anywhere
Here, public is the keyword used to specify that the function has no restriction on being used
anywhere throughout the system.
static: No object needed to run main()
void: Does not return any value
main() It is the function name. The main function is called by default.
throws IOException: Handles unexpected I/O errors
It eliminates the I/O errors (if any) that creep in while programming.
Accepting Integer Input
• Use Integer.parseInt() with readLine() method
• Parses string input into 32-bit integer
int n;
System.out.println("Enter a number:");
n = Integer.parseInt(in.readLine());
Accepting Float and Double Values
float f; double d;
System.out.println("Enter a real number:"); System.out.println("Enter a real number:");
f = Float.parseFloat(in.readLine()); d = Double.parseDouble(in.readLine());
Accepting a Character
Characters are read using read() method.
Needs to be explicitly cast to char type.
char ch;
System.out.println("Enter a character:");
ch = (char) in.read();
Accepting a String
Strings are directly accepted using readLine().
String str;
System.out.println("Enter a string:");
str = in.readLine();
Prog. 1 In a class of 'n' number of students, the number of girls is 'm'. Write a
program to input the values of n and m. Find and display the percentage of
boys and girls in the class.
// To find and display the percentage of boys and girls
import java.io.*;
class Number
{
public static void main(String args[]) throws IOException
{
InputStreamReader read = new InputStreamReader(System.in); b=n-m;
BufferedReader in = new BufferedReader(in); perb=(double)(b/n)*100;
int n,m,b; perg=(double)(m/n)*100;
double perb, perg; System.out.println("Percentage of boys = "+perb);
System.out.println("Enter number of students and girls"); System.out.println("Percentage of girls = "+perg);
n=Integer.parseInt(in.readLine()); }
m=Integer.parseInt(in.readLine()); }
Prog. 2 Write a program in Java to accept the diagonal of a square. Find and display
the area and perimeter by using Input Stream.
// To find and display the area and perimeter by using Input Stream
import java.io.*;
public class Square
{
public static void main(String args[]) throws IOException a = d/Math.sqrt(2)
{ ar=a*a;
InputStreamReader read=new InputStreamReader(System.in); p=4*a;
BufferedReader in=new BufferedReader(read); System.out.println("The area = "+ar);
double a,d,p,ar;
System.out.println("The perimeter = "+p);
System.out.println("Enter the diagonal of a square");
}
d=Double.parseDouble(in.readLine());
}
Summary and Key Takeaways
• InputStreamReader converts byte stream to character stream.
• BufferedReader allows fast, buffered reading of text.
• Both classes work together to enable efficient and flexible input handling.
• Suitable for handling various data types from the console.
• More reliable than Scanner in certain complex I/O operations.