Java Scanner delimiter() Method

The delimiter() method in Java, part of the java.util.Scanner class, is used to retrieve the delimiter pattern that the Scanner object is currently using to tokenize the input.

Table of Contents

  1. Introduction
  2. delimiter() Method Syntax
  3. Understanding delimiter()
  4. Examples
    • Basic Usage
    • Custom Delimiter
  5. Real-World Use Case
  6. Conclusion

Introduction

The delimiter() method returns the Pattern that the Scanner is currently using to tokenize the input. By default, the Scanner uses whitespace as the delimiter.

delimiter() Method Syntax

The syntax for the delimiter() method is as follows:

public Pattern delimiter()

Parameters:

  • This method does not take any parameters.

Returns:

  • The Pattern the Scanner is currently using to tokenize the input.

Understanding delimiter()

The delimiter() method allows you to get the current delimiter pattern of the Scanner. The delimiter pattern is used to separate the tokens in the input stream.

Examples

Basic Usage

To demonstrate the basic usage of delimiter(), we will create a Scanner object and retrieve its default delimiter pattern.

Example

import java.util.Scanner;
import java.util.regex.Pattern;

public class ScannerDelimiterExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Pattern delimiter = scanner.delimiter();

        System.out.println("Default delimiter: " + delimiter.pattern());

        scanner.close();
    }
}

Output:

Default delimiter: \p{javaWhitespace}+

Custom Delimiter

This example shows how to set a custom delimiter for the Scanner and then retrieve it using the delimiter() method.

Example

import java.util.Scanner;
import java.util.regex.Pattern;

public class CustomDelimiterExample {
    public static void main(String[] args) {
        String input = "apple,banana,cherry";
        Scanner scanner = new Scanner(input);
        scanner.useDelimiter(",");

        while (scanner.hasNext()) {
            System.out.println(scanner.next());
        }

        Pattern delimiter = scanner.delimiter();
        System.out.println("Custom delimiter: " + delimiter.pattern());

        scanner.close();
    }
}

Output:

apple
banana
cherry
Custom delimiter: ,

Real-World Use Case

Parsing CSV Files

In real-world applications, the Scanner class can be used to parse CSV files by setting a custom delimiter.

Example

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CSVParsingExample {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(new File("data.csv"))) {
            scanner.useDelimiter(",");

            while (scanner.hasNext()) {
                String data = scanner.next();
                System.out.print(data + " | ");
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + e.getMessage());
        }
    }
}

In this example, the Scanner is configured to use a comma (,) as the delimiter, making it suitable for parsing CSV files.

Output (Assuming data.csv contains "apple,banana,cherry"):

apple | banana | cherry |

Conclusion

The Scanner.delimiter() method is useful for retrieving the current delimiter pattern used by the Scanner to tokenize input. By understanding and using this method, you can efficiently parse and process different types of input data, such as space-separated or comma-separated values. Whether you are working with default or custom delimiters, the delimiter() method provides a reliable way to handle tokenization in your Java applications.

Comments