What is scanner class in Java?

The scanner is a class in the java.util package that is used to take input of primitive types such as int, double, etc., and string. This is the easiest way to read input into a Java program, but not very effective when you need input methods for time-limited scenarios such as competitive programming.

  • To create an object of the Scanner class, we usually pass an object that has been defined by System.in, which is the standard input stream. We can pass an object of class File if we want to read input from a file.
  • To read numeric values ??from a particular XYZ data type, the function to be used is nextXYZ(). For example, to read a value of type short, we can use nextShort().
  • To read strings, we use nextLine().
  • To read characters we use next(). CharAt(0). The next() function returns the next character/word in the input as a string, and the charAt(0) function returns the first character in the string.

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

Input :

Geek
F
40
9876543210
9.9

Output :

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

Sometimes we need to check if the next value we read is of a certain type or if the input is complete (EOF tag found).

We then check if the scanner input is the type we want by using the hasNextXYZ() function, where XYZ is the type we are interested in. The function returns true if the scanner has a flag of this type, otherwise false. For example, in the following code, we use hasNextInt(). To search for a string, we use hasNextLine(). Similarly, we use hasNext() to search for characters. CharAt(0).

Let's take a look at the code snippet to read some numbers from the console and print the average.

// 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:

Mean: 397

 

Submit Your Programming Assignment Details