A Beginner’s Guide to Using the Scanner Class in Java
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 classesConstantsDatatypeMethodIntegerbytex=s.nextByte();shortx=s.nextShort();intx=s.nextInt();longx=s.nextLong();Floating pointfloatx=s.nextFloat();doublex=s.nextDouble();Charactercharx=s.next().charAt(0);StringStringx=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

Comments
Post a Comment