Write a program to show the different ways to reading input from user in javaThere are three ways be which we can read input from user on every datatype.
1. Using Buffered Reader class>
2. Using Scanner class
3. Using Console Class
USING BUFFERED READER CLASS:-
import java.io.*;
class addition
{
public static void main(String rags[])
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a,b,c;
System.out.println("Enter Any Two Number: ");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=a+b;
System.out.println("Addition of two number : "+c);
}
catch(Exception er)
{
System.out.println(er);
}
}
}
USING SCANNER CLASS:-
import java.util.Scanner;
class addition
{
public static void main(String rags[])
{
try
{
Scanner scn = new Scanner(System.in);
int a,b,c;
System.out.println("Enter Any Two Number: ");
a=scn.nextInt();
b=scn.nextInt();
c=a+b;
System.out.println("Addition of two number : "+c);
}
catch(Exception er)
{
System.out.println(er);
}
}
}
USING CONSOLE CLASS:-
import java.io.*;
class addition
{
public static void main(String rags[])
{
try
{
Console con=System.console();
int a;
float b,c;
//input in float and int using console class
System.out.println("Enter Any Two Number: ");
a=Integer.parseInt(con.readLine());
b=Float.parseFloat(con.readLine());
c=a+b;
System.out.println("Addition of two number : "+c);
}
catch(Exception er)
{
System.out.println(er);
}
}
}
Use the differed way of input from user in java How to take input in float value using different classs in java Input value from user in java Take user input value in float variable
ConversionConversion EmoticonEmoticon