Scanner Class In Java


Java / Saturday, August 11th, 2018

Scanner Class

The Scanner Class in java allows user to input the values of various data types. The scanner class is defined within a package named java.util. There are many methods in the scanner class that we need to take input for various data types. It does not require the limit of values to be mentioned in advance by the users. The end of values can be determined by a sentinel character or string. In order to use scanner class in java programming, you need to install atleast jdk 1.5 or above versions of jdk. This class is helpful in inputting different types of numeric and non-numeric data. However, the output statement remains the same i.e., System.out.print() or System.out.println().

Importing the System Package java.util

You must import java.util package to be able to use all the facilities of the scanner class. The process of importing is shown below:

Scanner class in java 01

Creating Scanner Object:

After importing the java.util package, you can use the scanner class and creating an object which can hold a set of values of different types.

Scanner class in java 02

Let us understand the above syntax,

1. ‘sc’ is an object of type Scanner (you can use any object name of your choice)
2. The new keyword can be used for dynamic allocation of the scanner object.
3. The scanner class has a constructor which is automatically invoked to store values in the scanner object from the console.
4. The parameter ‘System.in’ allows the constructor to receive data from the keyboard.

Advantages of input through scanner class

1. The user doesn’t need to mention the set of data being input from the console.
2. InputStreamReader and BufferReader are not required to be mentioned.
3. The program logic is simple because the scanner class has various methods to manipulate input data.
4. String manipulation is easier as each word can be obtained as a token and handled separately.
5. It has an easier approach to read records from a data file.

Methods to read data from the scanner object

nextInt()

It receives an integer from the scanner object. It is stored in an integer type variable.
Syntax:  int <var_name>  = < scanner_ob>.nextInt();
Example:
Scanner sc = new Scanner(System.in);
int p = sc.nextInt();
This means that the variable p stores an integer through the scanner object ‘sc’.

nextFloat()

It receives floating type value and stores it into a floating type variable.
Syntax:  float <var_name>  = < scanner_ob>.nextFloat();
Example:
Scanner sc = new Scanner(System.in);
float p = sc.nextFloat();
This means that the variable p stores a float type value through the scanner object ‘sc’.

nextLong()

This method receives long type value and stores it into a long type variable.
Syntax:  long <var_name>  = < scanner_ob>.nextLong();
Example:
Scanner sc = new Scanner(System.in);
long p = sc.nextLong();
This means that the variable p stores a long type value through the scanner object ‘sc’.

nextDouble()

This method receives the double type value and stores it into a double type variable.
Syntax:  double <var_name>  = < scanner_ob>. nextDouble();
Example:
Scanner sc = new Scanner(System.in);
double p = sc. nextDouble();
This means that the variable p stores a long type value through the scanner object ‘sc’.

next()

This method accepts the next token which is string from the scanner object. A token can be defined by a set of characters between two simultaneous whitespaces or delimiters.
Syntax: String <var_name> = <scanner_ob>.next();
Example:
Scanner sc = new Scanner(System.in);
String str = sc.next();
This means that the variable str stores the next token as a string from the scanner object.

nextLine()

this method accepts the entire line of text from the scanner object.
Syntax: String <var_name> = <scanner_ob>.nextLine();
Example:
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
This means that the variable str stores a line of text from the scanner object.

 Program 01

Here is the sample program

/**
 *
 * @author Rajib
 */
import java.util.Scanner;

public class ScannerClass {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int a;
        double b;
        String line;
        System.out.print("Enter an integer: ");
        a = sc.nextInt();
        System.out.print("Enter a double: ");
        b = sc.nextDouble();
        System.out.print("Enter a Line: ");
        line = sc.next();        
        
        System.out.println("You have entered the following data: ");
        System.out.println(a);
        System.out.println(b);        
        System.out.println(line);        
    }    
}

 

<< Previous     Next>>

 ;

Leave a Reply

Your email address will not be published. Required fields are marked *