Scanner Class in Java

Scanner class in Java is a part of the java.util package and is used to read and parse different types of input from various sources. It provides methods to read data from different input streams, such as the console, files, and other input sources, and it can parse the input into different data types like integers, floats, and strings.

Scanner Class in Java
package com.java.io;

import java.util.Scanner;

public class ScannerExample {

    public static void main(String[] args) {

        // Create a Scanner object
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter your name");

        String name = scanner.nextLine();
        System.out.println("Your name is: " + name);

        System.out.println("Please enter your age");

        Integer age = scanner.nextInt();
        System.out.println("Your age is: " + age);

        System.out.println("Please enter your salary");
        Double salary = scanner.nextDouble();
        System.out.println("Your name is: " + salary);

        System.out.println("Thanks for all your information.");
        System.out.println("Shutting down.");

        scanner.close();

    }

}

Output

Screen Shot 2020-04-12 at 5.21.05 PM

Methods of Scanner Class in Java

Some commonly used methods of the Scanner class in Java are mentioned below:

MethodDescription
nextBoolean()Reads a boolean value from the input.
nextByte()Reads a byte value from the input.
nextShort()Reads a short value from the input.
nextInt()Reads an integer value from the input.
nextLong()Reads a long value from the input.
nextFloat()Reads a float value from the input.
nextDouble()Reads a double value from the input.
nextLine()Reads a line of text from the input, including spaces.
next()Reads a single word (token) from the input.
useDelimiter(String)Sets the delimiter to be used for tokenization.
hasNext()Checks if there’s another token in the input.
hasNextLine()Checks if there’s another line of text in the input.
close()Closes the scanner.

Real World Use cases of Scanner Class in Java

The Scanner class in Java is used for reading input from various sources such as the console, files, and other input streams. It provides a convenient way to parse different types of data from these sources. Here are some real-world use cases of the Scanner class in Java:

  1. Console Input: One of the most common use cases for the Scanner class is to read input from the console. This allows users to interact with a Java program by entering values via the keyboard. For example, you might use it to read user names, numbers, or other inputs.
  2. File Reading: The Scanner class can also be used to read data from files. You can use it to read and process the contents of text files, CSV files, configuration files, and more. This is useful for tasks such as reading configuration settings, analyzing log files, or processing data stored in files.
  3. Parsing Data: The Scanner class provides methods for parsing different types of data, such as integers, floats, and strings. You can use it to extract and convert specific types of data from a text source. For example, you could use it to read and process a list of numbers from a file.
  4. Tokenization: The Scanner class can tokenize (split into smaller parts) input based on delimiters. This is useful for processing text that is organized into sections, such as CSV files or data with a specific format.
  5. Validation and Error Handling: When reading user input, you can use the Scanner class to validate and handle errors. For example, you can check if the entered input matches expected formats or ranges and prompt the user to enter valid data if necessary.
  6. Interactive Menu Systems: When building console-based menu-driven applications, the Scanner class can be used to read user choices and execute corresponding actions. This is often seen in simple text-based games or utility programs.
  7. Language Processing: While more advanced language processing typically requires more sophisticated libraries, the Scanner class can be used for simple text analysis tasks, like counting occurrences of certain words or phrases in a text.
  8. Data Input Validation: In applications that require data input from users, the Scanner class can be used to ensure that the entered data meets specific requirements or formats before it is used by the program.

Conclusion: Scanner Class in Java

In conclusion, the Scanner class in Java is a versatile utility that provides a convenient way to read and parse input from various sources, such as the console, files, and other input streams. It is commonly used for tasks such as reading user input, processing text files, and extracting specific data from different formats.

Leave a Reply

Your email address will not be published. Required fields are marked *