Scanner Class in Java


Input by using Scanner Class

 The Scanner Class is defined within a Java Package(java.util.*; OR  Java.util.Scanner).There are more methods in class Scanner .

Creating Scanner Object

Scanner Class is available in system package java.util . You must import this package to avail the facilities contained in Scanner Class.



After importing this package you have got facility to vail scanner class and create object which can hold a set of values of different types. The syntax to create a Scanner class is as shown below:

Scanner sca=new Scanner(;                                                                                    



Sca---Scanner Object Operator

New—New Operator



Declare an object of type Scanner say ‘sca’(U can use any object name of ur choice).

New keyword can be used to have dynamic declaration of scanner object.

The Scanner class has a special constructor which is automatically invoked to store values in the Scanner object from Console.

The parameter “” allows the constructor to receive data from the keyboard.

The Scanner Class basically works on the Principle of  TOKENS in the input. A token is a series of characters that ends with a delimiter. Some delimiters which can be used as token separator are comma(,),full stop(.),semi-colon(;),whitespace etc. Scanner class uses white space as default token separator. A whitespace character can be a blank, a tab character, a carriage return, or the end of the file.

DELIMITERS----(,) (; )(.)( ?)( !)( Whitespace)

                       Now, you are well aware that default delimiter is referred to be white spaces but other delimiters can also be set for scanner object in order to separate the tokens. The process as shown below:


                  <Scanner Object>. useDelimiter(delimitr);



Advantages of Input through Scanner Class

1..User may not need to mention set of data being input from the console.The end of data elements can be determined through a special token.

2..It provide an easier approach to read records from a data file.

3.. Program Logic is simple because scanner class has various methods to manipulate input data.

4.. String manipulation is quite easier as each word can be obtained as a token and handled separately.




         1....nextInt();   ----It receive the next token from scanner object which can be

                                             expressed as an integer and stored in integer type variable.




             Scanner si=new Scanner(;

                     int g;


It means the variable g stores as an integer through the object.


      2...   nextFloat(); ----It receive the next token which can be expresses as a floating type

                                                value and stores it ito floating type variable.


                                Float<variable>=<Scanner Object>.nextFloat();


                Scanner si=new Scanner(;

                     Float er;


   It means the variable er stores a float value through the scanner object.


        3.... nextLong(); ---This method receives the next token expressed as a long type

                                              value and stores it into a long type variable.


                    Long<variable>=<Scanner Object>.nextLong();


                    Scanner si=new Scanner(;

                              Long r;


     It means the variable r stores a long value through the scanner object.


        4... nextDouble(); ----This method receives the next token which can be expressed as

                                                a double type value and stores it into a double type variable.


                 Double<variable>=<Scanner Object>.nextDouble();


             Scanner si=new Scanner(;

                      double r;


         It means the variable r stores a double value through the scanner object.


         5..... Next(); ---This method accepts the next token from the scanner object as

                                        astring.A token can be defined by a set of characters between two                                                   simultaneous white spaces or delimiters.


                      String<variable>=<Scanner Object>.next();


                  Scanner si=new Scanner(;

                            String r;


         It means the variable r stores the next token as a string from scanner object.


         6.... nextLine();--- The application of next()  method is to accept a token as a string

                                             from the  scanner object. When a line of text is input to the

                                            scanner  object the next() method  will receive the first token.

                                             Rest part of the text line can be obtained as a token by using

                                            nextLine() method. In  case, next() method is not applied

                                            before, the nextLines() method results in entire line of text.


                      String<variable>=<Scanner Object>.nextLine();


                    Scanner si=new Scanner(;

                          String r;


            It means the variable r stores a line of text through the scanner object.


import java.util.Scanner;

public class Salary


public static void main(String a[])


Scanner sc=new Scanner(;

int basic;

double da,hra,pf,np,gp;

String st;

System.out.println("Enter name of the employee and basic salary");








System.out.println("Name of the Employee::"+st);

System.out.println("Gross Pay Rs::"+gp);

System.out.println("Net Pay Rs:"+np);




scanner class in Java