Getting input from Keyboard.

In this article,Getting input from Keyboard is explained in detail with examples.
"Scanner" is a class, using which, we can accept input from standard input device i.e.Keyboard.
This class is available in java.util package.Such package need to be imported in a java program.
Ex 
import java.util.*; or
import java.util.Scanner;
To use this class,we create object of this class.
Ex
Scanner sc=new Scanner(System.in); //System.in defines standard input device i.e. Keyboard.
for getting the input from keyboard the statement should be in the format shown below
String ss=sc.next(); 
String ss=sc.nextline();     (From topic Getting input from Keyboard)
This class provides following methods, (From topic Getting input from Keyboard)

Datatype
Accepted value
next();
Accepts 1 word.
nextline();
Accepts 1 line of text.
nextBoolean();
Accepts Boolean value.
nextByte();
Accepts byte value.
nextShort();
Accepts short value.
nextInt();
Accepts int value.
nextLong();
Accepts long value.
nextFloat();
Accepts float value.
nextDouble();
Accepts double value.

Ex
//program related to Getting input from Keyboard--getting the input from keyboard.
import java.util.*;
class ScannerExample
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
String name;
System.out.print("Enter your name: ");
name=scan.next();
System.out.println("Given name is: "+name);// as we used next() we will get only1 word output.
}
}
Output
Getting Inputs from Keyboard-javaform

Ex
//program related to Getting input from Keyboard--getting the input from keyboard.
import java.util.*;
class ScannerExample
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
String name;
System.out.print("Enter your name: ");
name=scan.nextLine();
System.out.println("Given name is: "+name); // here the output is, total given words in single line.
}
}
Output
Input from Keyboard-javaform

Ex
//program related to Getting input from Keyboard--getting the input from keyboard.
import java.util.*;
class ScannerExample
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
int x;
System.out.print("Enter your age: ");
x=scan.nextInt();
System.out.println("Your age is: "+x);
}
}
output
Input of Age thorugh keyboard-javaform

Ex
//program related to Getting input from Keyboard--getting the input from keyboard.
import java.util.*;
class ScannerExample
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
float f;
System.out.print("Enter your percentage: ");
f=scan.nextFloat();
System.out.println("Your percentage is: "+f);
}
}
Output
Input of Percentage through keyboard-javaform

Ex
//program related to Getting input from Keyboard--getting the input from keyboard.
import java.util.Scanner;
class ScannerExample
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
boolean b;
System.out.print("Are you working (True/False): ");
b=scan.nextBoolean();
if(b) 
System.out.println("Your are employeed");
else
System.out.println("Your are Unemployeed");
}
}
Output
Input from Keyboard-javaform

Description of a class
From the command prompt,we can see description class using the following command.
Ex: javap -p java.util.Scanner
Here, 
javap is a command intimating JVM to provide the description of a class.
-p also show private numbers of a class. (From topic Getting input from Keyboard)
Other Examples
To see the description of predefined classes/ system we use.
javap -p java.lang.String //pre-defined classes.
javap -p java.lang.System.
To see description of userdefined classes/files in folder we use.
file path(d:java)\> javap -p Student //user defined classes
file path(d:java)\> javap -p Rectangle.

Continue to next topic Arrays in Java.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required