Java

How to import scanner in java?

Java is a well-known popular language that is used all over the world. Despite its unique features, the reason why most programmers like this language is because this language provides easy and flexible user interaction which is a remarkable thing for a programming language. Now there is a chance that you most likely think, how a user can interact with the program and provide input. For this purpose, Java provides us with a built-in class called the Scanner class.

In this write-up, we acknowledged regarding following outcomes

  • What is a scanner class in Java?
  • How do we import the scanner class in Java?

What is a scanner class in Java?

In Java, the scanner class is present in the java.util package. This class contains many methods that are used to get input from the user at runtime and uses different methods to get an integer, primitive type input from the user. Following are some popular and important methods used by this class to get user input

Methods Description
next() This method is used to get string type input.
nextInt() This method is used to get integer type input.
nextFloat() This method is used to get floating-point number input.
nextBoolean() This method is used to get Boolean type input.
nextShort() This method is used to get short integer value input.
nextLong() This method is used to get Long integer value input.
close() This method is used to close or terminate the scanner.

How do we import scanner class in Java?

As we mentioned above, the scanner class is a part of java.util package. So to import the scanner class in java we need to import the java.util.Scanner package first according to the following syntax.

Syntax:

Import java.util.Scanner

Before using the methods of the scanner class you must import this class along with its package by using the above syntax in order to inherit its properties.

Code:

package methods;
import java.util.Scanner;
public class sdemo {
  public static void main(String[] args) {
    Scanner get = new Scanner(System.in);
    System.out.println("");
    System.out.println("Enter Your Name : ");
    String n = get.next();
    System.out.println("Enter Your Email Address : ");
    String e = get.next();
    System.out.println("Enter Your Phone Number : ");
    Long num = get.nextLong();
    System.out.println("Enter Your Gender : ");
    char gen = get.next().charAt(0);
    System.out.println("\n" + "");
    System.out.println("Your Name is : "+ n);
    System.out.println("Your Email Address is : "+ e);
    System.out.println("Your Phone Number is : "+ num);
    System.out.println("Your Gender is :"+ gen + "\n");
    System.out.println("Confirm your information by pressing Y/N");
    char con = get.next().charAt(0);
    if (con == 'y') {System.out.println("Registration Successful");}
    else {System.out.println("Registration Failed");}
    get.close();
    }
}

In this code, first we import the scanner class to inherit its properties. Then the object is created for the Scanner class to inherit its properties and use its methods. Then we get user information by using next(), nextLong(), next().CharAt(0) methods of this class.

Output:

Output:

In the output, it is clearly seen that we get input from the user with the help of scanner class methods.

You have learned the purpose of scanner class and also how to use/import in Java.

Conclusion

In Java, the scanner class is imported by using the import keyword with java.util.Scanner which is the part of java.util and to use the methods of this class we need to create the object for this class first. In this article, we learned about the scanner class and its purpose. Also, we have gone through the process of importing the scanner class in Java with a detailed example for your better understanding.

About the author

Muhammad Huzaifa

I am a computer science graduate with a passion to learn technical knowledge and share it
with the world. I love to work on top state-of-the-art computing languages. My aim is to best
serve the community with my work.