Scanner classes in Java (Part 1)

Keywords: Java Programming Mobile

Scanner is a class in the java.util package that retrieves input of the original type, such as int, double, and strings. It's the easiest way to read input in Java programs, but it's not very effective if you want an input method to be used where time is a constraint in competitive programming.

To create an object of a Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. If we want to read input from a file, we can pass an object of the File class.
To read the value of a data type XYZ, the function to be used is nextXYZ (). For example, to read a value of type short, you can use nextShort ()
To read the string, we use nextLine ().
To read a single character, we use
next()
charAt(0)
next()
The function returns the next tag/word in the input as a string, and charAt (0) returns the first character in the string.
Let's look at code snippets 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(System.in);

        // String input
        String name = sc.nextLine();

        // Character input
        char gender = sc.next().charAt(0);

        // 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 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);
    }
}

Sometimes, we have to check whether the next value we read is of a certain type or whether the input has ended (encountering the EOF tag).?

Then, with the help of the hasNextXYZ () function, we check whether the input of the scanner is the type we want, and XYZ is the type we are interested in. If the scanner has this type of tag, the function returns true or false. For example, in the code above, we used hasNextInt (). To check strings, we use hasNextLine (). Similarly, to check individual characters, we use
hasNext()
charAt(0).

Let's look at code snippets that read some numbers from the console and print their meanings.

// 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(System.in);

        // 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;
            count++;
        }
        int mean = sum / count;
        System.out.println("Mean: " + mean);
    }
}

Input:

101
223
238
892
99
500
728

Output:

Average:
397

Posted by snorcha on Fri, 19 Apr 2019 14:15:34 -0700