Java

How to get User Input in Java using scanner class

One of the remarkable things in a programming language is that we can write programs to which users can interact. Java programming allows a user to enter data by means of the Scanner class. It is a built-in class in java that is present in the java.util package. The Scanner class provides several methods that can be used to achieve different functionalities such as reading, parsing the data, etc. In java, the scanner class is one of the simplest, easiest, and most widely used ways of getting input from users.

This write-up presents a comprehensive overview of how to get user input in java, and in this regard, this write-up will cover the following aspects of Scanner Class.

So let’s get started!

Java Scanner Class

It belongs to java.util package and can be used to get the input of String and primitive types such as int, char, float, etc. In order to work with the Java Scanner class, we have to follow the below-given steps:

  1. Import the scanner class,
  2. Create the object of the Scanner class.
  3. Utilize the inbuilt methods of the Scanner class to take the user’s input.

How to Import Scanner Class

Initially, we have to import the Scanner class into our project and to do so, we have to write the following piece of code:

Import java.util.Scanner

How to Create Object of Scanner Class

Importing the scanner class will allow us to create the object of the scanner class and to do so we need to follow the below-given syntax:

Scanner scan = new Scanner(System.in);

Here in the above code-snippet the System.in is a predefined object that represents input-stream.

Various Methods of Scanner Class

Up till now we are done with importing the scanner class and creating the object of that class in our project. Now, we can utilize any of the built-in methods of the Scanner class such as next(), nextLine(), nextShort(), and many more.

To read any numeric data or short data, all you have to do is simply specify the data type along with “next” followed by parentheses as shown below:

nextInt() method to take an integer value,

nextShort() method to get a value of short data type and so on.

One of the most significant and widely used methods of Scanner class is the nextLine() method which is used to read the strings.

Practical Implementation of Scanner Class in Java

For a profound understanding let’s implement the above-mentioned concepts in an example.

Example

The below-given code will provide a better understanding of how to get input from users using the Scanner class:

import java.util.Scanner;
public class UsersInput {
  public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);  
      System.out.println("Please Enter the Name of Employee: ");
      String empName = scan.nextLine();
      System.out.println("Please Enter the ID of Employee: ");
      int empId = scan.nextInt();
      System.out.println("Employee Name :" + empName);
      System.out.println("Employee ID :" + empId);
   }
}

In the above snippet, we take employee Name and Id from the user, the complete code and respective output is shown in the following screenshot:

The output authenticates that the working of scanner class as it takes the data from the user successfully.

Conclusion

In Java, in order to take input from users all you need to do is import the Scanner class of java.util package, then create the object of that class and utilize the built-in methods of the class to perform different functionalities. The Scanner class provides a wide range of methods to read values of various data types e.g. nextLine(), nextInt(), and nextByte() methods can be used to read String, integer, and byte data respectively from the user. There are many more methods/functions that can be used for various purposes. This write-up presents a thorough understanding of what is Scanner class, and how to work with scanner class.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.