Scanner class

Scanner Class:

Scanner is a predefined class in java language which is used to read the values form the keyboad.This Scanner class Comes from 'util package'.

Steps to read the data from the keyboard using scanner

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()
import java.util.*;
class Addition
{
 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:
    Java Addition 
    Enter x,y values
    12
    12
    Addition  Is 24