Upload
simarsimmygrewal
View
162
Download
2
Embed Size (px)
Citation preview
•Scanner class is one of the class of Java
which provides the methods to get inputs.
•Scanner class is present in java.util package
so we import this package in our program.
•We first create an object of Scanner class and
then we use the methods of Scanner class.
Scanner Class to receive input
Scanner a = new Scanner(System.in);Following are methods of Scanner class:
•next( ) to input a string
•nextByte() to input a byte value
•nextInt( ) to input an integer
•nextFloat( ) to input a float
•nextLong( ) for Long Values
•nextDouble() for double values
Scanner Class to receive input
import java.util.Scanner;
class GetInput {
public static void main(String args[]) {
int a;
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);
} }
To Receive Integer Input
import java.util.Scanner;
class GetInput {
public static void main(String args[]) {
float b;
Scanner in = new Scanner(System.in);
System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);
} }
To Receive float Input
import java.util.Scanner;
class GetInput {
public static void main(String args[]) {
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string"+s);
} }
To Receive String Input