Thursday, 18 March 2021

InputStreamReader and Console Class in Java

 

Take Input from User

Using InputStreamReader Class of java.io package


import java.io.*;

class In_5

{

public static void main(String[] args)

{

try

{

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

System.out.print("Enter First Name: ");

String s1=br.readLine();

System.out.print("Enter Second Name: ");

String s2=br.readLine();

System.out.println("Name="+(s1+" " +s2));

}

catch(Exception ex){}

}

}


Parsing The Input


import java.io.*;

class In_6

{

public static void main(String[] args)

{

try

{

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

System.out.print("Enter First Number: ");

int a=Integer.parseInt(br.readLine());

System.out.print("Enter Second Number: ");

int b=Integer.parseInt(br.readLine());

System.out.println("Sum="+(a+b));

}

catch(Exception ex){}

}

}


Console Class of java.io package


import java.io.*;

class In_7

{

public static void main(String[] args)

{

Console c=System.console();

System.out.println("Enter Your Name: ");

String s=c.readLine();

System.out.println("Your name is "+s);

}

}

No comments:

Post a Comment