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(System.in);                                                                                    

Explain……

Scanner—Package

Sca---Scanner Object Operator

New—New Operator

Scanner(System.in)----Constructor

 

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 “System.in” 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:

Syntax:

                  <Scanner Object>. useDelimiter(delimitr);

e.g.,

                 scn.useDelimiter(“,”);

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.

 

 

METHODS TO READ TOKENS FROM SCANNER OBJECT

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

                                             expressed as an integer and stored in integer type variable.

                    Syntax:     

               Int<variable>=<scannerObject>.nextInt();

      e.g.,

             Scanner si=new Scanner(System.in);

                     int g;

                     g=si.nextInt();

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.

       Syntax:

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

       e.g.,

                Scanner si=new Scanner(System.in);

                     Float er;

                     er=in.nextFloat();

   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.

  Syntax:

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

   e.g.,

                    Scanner si=new Scanner(System.in);

                              Long r;

                        r=in.nextLong();

     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.

   Syntax:

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

  e.g.,

             Scanner si=new Scanner(System.in);

                      double r;

                      r=in.nextDouble();

         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.

           Syntax:

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

        e.g.,

                  Scanner si=new Scanner(System.in);

                            String r;

                             r=in.next();

         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.

          Syntax:

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

        e.g.,

                    Scanner si=new Scanner(System.in);

                          String r;

                           r=in.nextLine();

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

EXAMPLE.........

import java.util.Scanner;

public class Salary

 {

public static void main(String a[])

{

Scanner sc=new Scanner(System.in);

int basic;

double da,hra,pf,np,gp;

String st;

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

st=sc.nextLine();

basic=sc.nextInt();

da=basic*30.0/100.0;

hra=basic*12.0/100.0;

pf=basic*8.33/100.0;

gp=basic+da+hra;

np=gp-pf;

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

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

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

}

}

OUTPUT.......

Scanner class in Java