Taking input from user in java
Taking User Input
User input is the most basic and the most important action of any application . In our last tutorial we have seen programs of addition ,subtraction,multiplication and division but the basic problem in all of those program was there was no provision for user input moreover they do calculation on only two specific numbers (4 and 5 in that case) . Now in this page we will see different classes and streams provided in java to take text input from user through console (output screen ) . Lets proceed. . . . .
1) Using method (function)call method
This method is very simple . You just have to put in the variables along with data type in between the paranthesis of the method(function).
Eg: <returntype><method name>(datatype1 variable1 ,datatype2 variable 2,. . . . . . )
Below is a simple program which user the above method to input an integer, an double,a string ,a character and a Boolean value from the user.
class Input1
{
public static void main(int a,double b,String str,char ch, boolean flag)//method with parameters to input data from console
{
//print the data back on the output screen
System.out.println("The integer number is "+a);
System.out.println("The double number is "+b);
System.out.println("The String is "+str);
System.out.println("The character is "+ch);
System.out.println("The boolean value is "+flag);// note boolean value can either be true or false
}//closing of main()
}// closing of class
This program when execute will generate a method call window which will promt user to input the required values and then it will show the output.
2) Using InputStreamReader and BufferedReader classes .
There two classes provided by java are capable of input any type of text data (int , float ,byte ,short,long ,double ,String , char and boolean) . In order to input data you have to follow there steps :-
a) Create the object of InputStreamReader class and then you have to link it to the object of BufferedReader class this will create a stream which could input data.
InputStreamReader isr = new InputStreamReader(System.in) ;
BufferedReader br = new BufferedReader(isr);
BufferedReader br = new BufferedReader(isr);
OR
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Through above code we have created the object of InputStreamReader (i.e. isr ) and connected it to BufferedReader object (i.e. br) now we can use br to invoke predefined functions read() and readLine() which will input data from console.
b) read()- This funcion is used to input a character from the console.
char ch = (char)br.read();
In above code ch is a character variable .Here (char) is to type cast the input of br.read() into character type.
readLine() - This function is used to input a string(i.e. text) from the user.
String str= br.readLine();
In above code str is a String variable and br.readLine() input a string.
c) In order to convert Text input ie(String ) into other types i.e.(int,float,double ext.) parse is used lets see how .
Syntax:- <data type> <variable name>= <DataType_ in _full>.parse<Datatype>(br.readLine());
Lets see how we can use it to input different data types.
#) To input short integer:
short num=Short.parseShort(br.readLine());
#) To input integer value :
int num =Integer.parseInt(br.readLine());
#) To input long value :
long num=Long.parseLong(br.readLine());
#) To input float value:
float num=Float.parseFloat(br.readLine());
#) To input double value
Double num = Double.parseDouble(br.readLine());
#) To input boolean value
boolean num = Boolean.parseBoolean(br.readLine());
Now we will make the same program which we have seen in function(method) call method by using this method and let's see the changes.
import java.io.*; /*Both the classes InputStream and BufferedReader are stored in predefined io(Input output) package so we have to import it*/
{
public static void main(String args[])throws IOException// throws the input output exception generated
{
int a;//variable to store integer value
double b;//variable to store double value
String str;//variable to store String value
char ch;//variable to store character value
boolean flag;//variable to store booleanvalue
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter an integer number :");
a=Integer.parseInt(br.readLine());
System.out.print("Enter an double number :");
b=Double.parseDouble(br.readLine());
System.out.print("Enter an string ");
str=br.readLine();
System.out.print("Enter an character ");
ch=(char)br.read();
System.out.print("Enter an boolean value ");
flag=Boolean.parseBoolean(br.readLine());
System.out.println("The Integer number is "+a);
System.out.println("The double number is "+b);
System.out.println("The String is "+str);
System.out.println("The Character is "+ch);
System.out.println("The Boolean value is "+flag);
}//closing of main()
}// closing of class
Comments
Post a Comment