INTRODUCTION:
Heii friends..In this tutorial I am going to show you to how
to get the input from the user.
WAYS TO GET THE INPUT FROM THE USER..
There are 2 ways to get the input from the user. Let’s
discuss one by one..
USING DATAINPUTSTREAM :
Using DataInputStream is one way to get the input. I will
give a step by step explanation of using DataInputStream
Initially every Java Program will start with a class..
Class Happy
{
}
And every class will have a public static void main(String
args[])
So the actual syntax will be,
Class Happy
{
Public static void main(String args)
{
}
}
So, our actual target is to get the input from the user
using DataInputStream.
EXAMPLE:
Class Happy
{
public static void main(String args)
{
int a, b; ---------------------------------------
>>>> 1
DataInputStream dis = new DataInputStream(System.in );
a= Integer.ParseInt(dis.readLine() );
System.out.println(“The value user printed is” +a);
}
}
EXPLANATION:
1 ------ > Here we are declaring 2 variables of Integer
data type.
Then we are using DataInputStream , creating an object for
it and the above is the actual syntax for using it.
Then comes the code a = Integer.ParseInt(dis.readLine() );
“a” is the variable which holds the value the user enters .
But what’s the code Integer.ParseInt(dis.readLine()
); doing there..?
Fine.. compiler usually will consider all the inputs we
enter in a String format. If we provide the Integer value as an Input then
Compiler will get baffled and will scold developers for this awful code..!!
So
what we have planned to do is, we are converting the “Integer input to String
format “ and we are passing to the Compiler and so the code ,
Integer.ParseInt(we are parsing to the String format from the Integer format)
OUTPUT:
So what the output will be?? Can you guess??
Itss Compile time error..
But Why..??
Because when we use DataInputStream , we must either
surround the code with try/ catch or add Throws declaration in main method. The
compiler is expecting to protect the code when an exception occurs.
So, after adding desired things the output actually will be,
Class Happy
{
public static void main(String args) throws IOException
{
int a;
DataInputStream dis = new
DataInputStream(System.in );
System.out.println(“Enter any value : ”);
a=
Integer.ParseInt(dis.readLine() );
System.out.println(“The value user entered is” +a);
}
}
OUTPUT:
Enter any value :
2 -- >> User
entered
The value user entered is 2
HOW TO GET STRING AS
AN INPUT FROM THE USER..??
Hmm.. getting Integer, Decimal etc from the user involves
similar procedure but what if we want to get the String as an Input from the
user..??
WHAT IS A STRING..??
A string is nothing but Sequence of characters.If you like
to more about string class and its methods please refer the Strings in JAVA
To declare a String datatype,
String a;
is the syntax.
The code now changes to,
Class Happy
{
public static void main(String args) throws IOException
{
String a;
DataInputStream
dis = new DataInputStream(System.in );
System.out.println(“Enter any string : ”);
a= dis.readLine() ;
System.out.println(“The value user entered is” +a);
}
}
EXPLANATION:
Initially we are declaring a String data type and
DataInputStream remains same because it’s the way we are using to get the input
from the user.
But look the code
A= dis.readLine() ;
We are not using Integer.ParseInt
here as we are not converting the Integer data type to String because we are
passing only the String data type. So, we are using only dis.readLine( )
This code gets the String input from the user and prints it
in console.
OUTPUT:
Enter any string :
Hai I am anu
The value user entered is Hai I am anu.
ANOTHER WAY OF GETTING INPUT FROM THE USER..
Another way of getting the input from the user is using
“SCANNER”
The Syntax is as follows,
Scanner scan = new Scanner(System.in);
String s= Scan.readLine();
The above syntax is to get the input in a String format. If
we want to get in an Integer format then use,
String s =Scan. nextInt( );
If we want to get the
double data type input from the user then we can use,
String s1 = Scan.nextDouble( );
EXAMPLE:
Class Happy
{
public static void main(String args) throws IOException
{
String a;
int b;
double c;
System.out.println(“Enter 3 vales – string , integer, double
: ”);
Scanner scan = new Scanner(System.in);
a= Scan.readLine(); // TO GET THE STRING INPUT
b= Scan.nextInt( );
c= Scan.nextDouble( );
System.out.println(“The string user entered is” +a);
System.out.println(“The integer user entered is” +b);
System.out.println(“The double user entered is” +c);
}
}
OUTPUT:
Enter 3 vales – string , integer, double :
Boo ------------------ input
5 --------------------- input
5.2000 ---------------- input
The string user entered is Boo
The integer user entered is 5
The double user entered is 5.2000
EXPLANATION:
So..this is another way of getting input from the user. This
method is widely used since it is user friendly.
CONCLUSION:
So.. that’s it for this post. Please leave your valuable
comments below . It will help me improve . Request for the post you wish is
welcomed. If you want some tutorials in Asp.Net and Android then email me. Stay
tuned for more posts. Have a blissful day :)
No comments :
Post a Comment