In Java, there are numerous built-in classes available to perform specific tasks. These classes play an essential role in performing user-required tasks in an effective manner. The Java classes are very helpful in making the job easier. One of the popularly used built-in classes is the Java “Scanner Class” which is responsible for fetching the user input.
In this write-up, we will document how the scanner class of Java works.
How to Use/Employ Scanner Class in Java?
The scanner class offers different built-in methods that take the input from the user and print the results accordingly. This class is a part of “java.util” package and is imported into a program through the import keyword. To implement the scanner class, create an object and utilize the methods available in the class according to the need. The table given below illustrates the built-in methods along with their description:
Function | Description |
---|---|
nextBoolean() | Prompts the user to enter a boolean value. |
nextByte() | Asks the user to type a byte value. |
nextDouble() | Asks the user to enter/type a double value. |
nextFloat() | Prompts the user to type a float value. |
nextLine() | Enables a user to specify/enter a line of text. |
nextLong() | Prompts the user to type a long value. |
nextShort() | Asks the user to enter a short value. |
next() | Prompts the user to type/provide a word. |
nextInt() | Prompts the user to type an integer value. |
Any of the above-stated methods can be used to get the user input of any particular type. Now, let us have a look at the code implementation of the Scanner class in Java.
Step 1: Import the Java Scanner Class
The Scanner package is imported for the user input.
import java.util.Scanner;
Step 2: Create an Object for the Scanner Class
A class is created and a Scanner object “ip” is declared in the main class.
//creating a new class
class scannerclass {
//main method
public static void main(String[] args) {
//initializing a new object
Scanner ip = new Scanner(System.in);
Step 3: Take Input from the User
Three inputs are taken from the user. The nextLine() method is used to read the specific line.
System.out.print("Enter your employeeId: ");
String id = ip.nextLine();
System.out.print("Type your fullName: ");String name = ip.nextLine();
System.out.print("Enter your Designation: ");
String desig = ip.nextLine();
Step 4: Use println() Method to Print the Output
The outputs for ID, Name, and Designation are displayed using the println() method for the specified inputs.
System.out.println("EmployeeId is " + id);System.out.println("Employee name is " + name);System.out.println("Employee Designation is " + desig);
Step 5: Use close() Method to Close the Scanner Class
The close() method closes the scanner class with the object of the scanner class.
ip.close();
}
}
Step 6: Review the Code
Combine the provided code snippets, review the complete code, and execute it to understand the working of Java’s Scanner class:
//Import the required package
import java.util.Scanner;
//Declaring a class
class scannerclass {
public static void main(String[] args) {
Scanner ip = new Scanner(System.in);
//Enter the ID
System.out.print("Enter your employeeId: ");
//The ID is printed using the nextLine() method
String id = ip.nextLine();
System.out.print("Type your fullName: ");
String name = ip.nextLine();
System.out.print("Enter your Designation: ");
String desig = ip.nextLine();
//The inputs are printed using the println() method
System.out.println("EmployeeId is " + id);
System.out.println("Employee name is " + name);
System.out.println("Employee Designation is " + desig);
//The close() method closes the current window.
ip.close();
}
}
Output
The below output shows the implementation of the Scanner class in Java.

This sums up the implementation of the Scanner class in Java.
Conclusion
To use/employ Java’s Scanner class, first, import the scanner class, make an instance of the class, and use one of the Scanner class functions to read the input from the user. The Scanner class belongs to the “java.util” package and provides a number of methods for reading different types of input, such as strings, integers, doubles, etc. In this blog post, we have illustrated the implementation of Scanner Class in Java using step-by-step instructions.