Open In App
Related Articles

Scanner Class in Java

Improve Article
Save Article
Like Article

In Java, Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings. Using the Scanner class in Java is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.

Java Scanner Input Types

Scanner class helps to take the standard input stream in Java. So, we need some methods to extract data from the stream. Methods used for extracting data are mentioned below:




Used for reading Boolean value                    


Used for reading Byte value


Used for reading Double value


Used for reading Float value


Used for reading Int value


Used for reading Line value


Used for reading Long value


Used for reading Short value

Let us look at the code snippet to read data of various data types.



// Java program to read data of various types using Scanner
// class.
import java.util.Scanner;
public class ScannerDemo1 {
    public static void main(String[] args)
        // Declare the object and initialize with
        // predefined standard input object
        Scanner sc = new Scanner(;
        // String input
        String name = sc.nextLine();
        // Character input
        char gender =;
        // Numerical data input
        // byte, short and float can be read
        // using similar-named functions.
        int age = sc.nextInt();
        long mobileNo = sc.nextLong();
        double cgpa = sc.nextDouble();
        // Print the values to check if the input was
        // correctly obtained.
        System.out.println("Name: " + name);
        System.out.println("Gender: " + gender);
        System.out.println("Age: " + age);
        System.out.println("Mobile Number: " + mobileNo);
        System.out.println("CGPA: " + cgpa);




Name: Geek
Gender: F
Age: 40
Mobile Number: 9876543210
CGPA: 9.9

Sometimes, we have to check if the next value we read is of a certain type or if the input has ended (EOF marker encountered).

Then, we check if the scanner’s input is of the type we want with the help of hasNextXYZ() functions where XYZ is the type we are interested in. The function returns true if the scanner has a token of that type, otherwise false. For example, in the below code, we have used hasNextInt(). To check for a string, we use hasNextLine(). Similarly, to check for a single character, we use hasNext().charAt(0).

Let us look at the code snippet to read some numbers from the console and print their mean. 


// Java program to read some values using Scanner
// class and print their mean.
import java.util.Scanner;
public class ScannerDemo2 {
    public static void main(String[] args)
        // Declare an object and initialize with
        // predefined standard input object
        Scanner sc = new Scanner(;
        // Initialize sum and count of input elements
        int sum = 0, count = 0;
        // Check if an int value is available
        while (sc.hasNextInt()) {
            // Read an int value
            int num = sc.nextInt();
            sum += num;
        if (count > 0) {
            int mean = sum / count;
            System.out.println("Mean: " + mean);
        else {
                "No integers were input. Mean cannot be calculated.");


1 2 3 4 5 


Mean: 3

Important Points About Java Scanner Class

  • To create an object of Scanner class, we usually pass the predefined object, which represents the standard input stream. We may pass an object of class File if we want to read input from a file.
  • To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read a value of type short, we can use nextShort()
  • To read strings, we use nextLine().
  • To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string.
  • The Scanner class reads an entire line and divides the line into tokens. Tokens are small elements that have some meaning to the Java compiler. For example, Suppose there is an input string: How are you
    In this case, the scanner object will read the entire line and divides the string into tokens: “How”, “are” and “you”. The object then iterates over each token and reads each token using its different methods.

If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials