☞As you know predefined classes are inside packages. So we have to import the package first.
☞InputStreamReader class is a member of java.io package.
☞To include the InputStreamReader class in our program, we need to import java.io package and the class.
import java.io.*;
☞To use the functions of InputStreamReader class, you need to create an object of InputStreamReader class and BufferedReaderClass.
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
☞As you can see in above lines, we have passed InputStreamReader Object i.e. isr in BufferedReader Object. So we can also write above two lines in a single line.
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
☞Firstly you will take input using <buffer_reader_object>.readLine() as a string.
☞Secondly you will convert this string into particular type using
<class_name>.parse<class_type>() function.
☞To convert inputted string into particular type, pass it as an argument in parse() function
☞Following are the functions of InputStreamReader class that you can use to input data of particular type. Syntax for getting input :
<data_type> <variable_name> = <class_type>.parse<class_type>(br.readLine())
Boolean.parseBoolean(br.readLine()) ➺ To input boolean value Byte.parseByte(br.readLine()) ➺ To input integer value Short.parseShort(br.readLine()) ➺ To input integer value Integer.parseInt(br.readLine()) ➺ To input integer value Long.parseLong(br.readLine()) ➺ To input integer value Float.parseFloat(br.readLine()) ➺ To input floating value Double.parseDouble(br.readLine()) ➺ To input double value readLine() ➺ To input string (char)<buffer_object>.read(); ➺ To input a character
import java.io.*; public class InputStreamClass { public static void main(String[] args)throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a boolean value : "); boolean x=Boolean.parseBoolean(br.readLine()); System.out.println("Enter an byte value : "); byte a=Byte.parseByte(br.readLine()); System.out.println("Enter an short value : "); short b=Short.parseShort(br.readLine()); System.out.println("Enter an integer value : "); int c=Integer.parseInt(br.readLine()); System.out.println("Enter a long value : "); long d=Long.parseLong(br.readLine()); System.out.println("Enter a float value : "); float e=Float.parseFloat(br.readLine()); System.out.println("Enter a double value : "); double f=Double.parseDouble(br.readLine()); System.out.println("Enter a String value : "); String g=br.readLine(); System.out.println("Enter a character value : "); char h=(char)br.read(); System.out.println("---------------------"); System.out.println(x); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); System.out.println(f); System.out.println(g); System.out.println(h); } }
Enter a boolean value : false Enter an byte value : 53 Enter an short value : 353 Enter an integer value : 59932 Enter a long value : 3005393833 Enter a float value : 64.45354 Enter a double value : 46.353545643 Enter a String value : we develop a passion for learning Enter a character value : & --------------------- false 53 353 59932 3005393833 64.45354 46.353545643 we develop a passion for learning &