5
•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 nner Class to receive in

Handling inputs via scanner class

Embed Size (px)

Citation preview

Page 1: Handling inputs via scanner class

•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

Page 2: Handling inputs via scanner class

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

Page 3: Handling inputs via scanner class

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

Page 4: Handling inputs via scanner class

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

Page 5: Handling inputs via scanner class

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