• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM


Scanner Class:
Scanner is a predefined class in java language which is used to Read the values form the keyboard.This Scanner class imports from 'util package'.
Steps to read the data from the keyboard using Scanner Class
Step-1:
import the util package
import java.util.*;

Step-2:
Create a object for the scanner class.
Scanner s=new Scanner(System.in);

Step-3:
Scanner class methods:
Scanner class classes
Constants Datatype Method
Integer byte x=s.nextByte();
short x=s.nextShort();
int x=s.nextInt();
long x=s.nextLong();
Floating point float x=s.nextFloat();
double x=s.nextDouble();
Character char x=s.next().charAt(0);
String String x=s.next();
x=s.nextLine();

Program Using Scanner Class


import java.util.*;
class Main
{
public static void main(String arr[])
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println("Enter x,y values");
x=s.nextInt();
y=s.nextInt();
z=x+y;
System.out.println("Addition is "+z);
}
}


OUTPUT:

Enter x,y values
12
12
Addition is 24


Key Points

Image