Scanner Class in Java

 The Scanner class is a part of the java.util package and is used for obtaining input of primitive data types such as int, double, etc., as well as strings. It is the simplest method for reading input in a Java program, although it may not be the most efficient choice in time-constrained scenarios like competitive programming.

To create an object of the Scanner class, we typically 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 numerical values of a certain data type, say XYZ, we use the nextXYZ() function. 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), where the next() function returns the next token or word in the input as a string, and charAt(0) returns the first character of that string.

The Scanner class reads an entire line and divides it into tokens. Tokens are small elements that have some meaning to the Java compiler. For instance, consider the input string "How are you". In this case, the scanner object will read the entire line and divide the string into tokens: "How", "are", and "you". The object then iterates over each token and reads each token using its various methods.

Let us examine a code snippet to read data of various data types:

java
import java.util.Scanner; public class ScannerDemo1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = sc.nextLine(); char gender = sc.next().charAt(0); int age = sc.nextInt(); long mobileNo = sc.nextLong(); double cgpa = sc.nextDouble(); 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); } }

In this example, we declare an object of the Scanner class and initialize it with the predefined standard input object. We then use various methods of the Scanner class to read inputs of different data types, such as String, char, int, long, and double. Finally, we print the values to ensure that the input was correctly obtained.

At times, we may need to check whether the next value we read is of a certain type or if the input has ended (i.e., the EOF marker has been encountered). In such cases, we can use the hasNextXYZ() functions of the Scanner class, where XYZ is the type we are interested in. These functions return true if the scanner has a token of that type; otherwise, they return false. For example, we can use hasNextInt() to check for an integer and hasNextLine() to check for a string. Similarly, we can use hasNext().charAt(0) to check for a single character.

Let us examine another code snippet to read some numbers from the console and print their mean:

java
import java.util.Scanner; public class ScannerDemo2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0, count = 0; while (sc.hasNextInt()) { int num = sc.nextInt(); sum += num; count++; } int mean = sum / count; System.out.println("Mean: " + mean); } }

In this example, we declare an object of the Scanner class and initialize it with the predefined standard input object. To create an object of the Scanner class, we typically pass the predefined object System.in, which represents the standard input stream. Alternatively, we can pass an object of the File class if we want to read input from a file.

To read numerical values of a certain data type XYZ, we use the function 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). The 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 it 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 divide the string into tokens: "How", "are", and "you". The object then iterates over each token and reads each token using its different methods.

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

Java
import java.util.Scanner; public class ScannerDemo1 { public static void main(String[] args) { // Declare the object and initialize it with the 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); } }

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). In such cases, 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 console and print their mean:

Java
import java.util.Scanner; public class ScannerDemo2 { public static void main(String[] args) { // Declare an object and initialize it with the 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); } }

 

Previous Post Next Post