Java Scanner findAll() Method

The findAll() method in Java, part of the java.util.Scanner class, is used to find all occurrences of a pattern in the input. It returns a stream of MatchResult objects, each representing a match of the specified pattern.

Table of Contents

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

Introduction

The findAll() method returns a stream of MatchResult objects, each representing a match of the specified pattern in the input. This method is useful for finding all matches of a pattern in the input text.

findAll() Method Syntax

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

public Stream<MatchResult> findAll(Pattern pattern)

Parameters:

  • pattern: The Pattern object representing the regular expression to be matched.

Returns:

  • A Stream<MatchResult> containing all matches of the specified pattern.

Understanding findAll()

The findAll() method searches the input for all occurrences of the specified pattern and returns them as a stream of MatchResult objects. Each MatchResult contains information about the match, such as the start and end positions of the match and the matched substring.

Examples

Basic Usage

To demonstrate the basic usage of findAll(), we will create a Scanner object, use it to search for all occurrences of a pattern in a string, and print the matches.

Example

import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Stream;

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

        Stream<MatchResult> matches = scanner.findAll(pattern);

        matches.forEach(match -> System.out.println("Found: " + match.group()));

        scanner.close();
    }
}

Output:

Found: apple
Found: apple

Using a Custom Pattern

This example shows how to use findAll() with a custom pattern to find all words that start with a specific letter.

Example

import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class CustomPatternExample {
    public static void main(String[] args) {
        String input = "Alice, Bob, Charlie, David, Alice, Bob";
        Scanner scanner = new Scanner(input);
        Pattern pattern = Pattern.compile("\\b[Aa]\\w*");

        Stream<MatchResult> matches = scanner.findAll(pattern);

        matches.forEach(match -> System.out.println("Found: " + match.group()));

        scanner.close();
    }
}

Output:

Found: Alice
Found: Alice

Real-World Use Case

Parsing Logs for Error Messages

In real-world applications, the findAll() method can be used to parse logs and find all occurrences of specific error messages or patterns.

Example

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class LogParserExample {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(new File("log.txt"))) {
            Pattern pattern = Pattern.compile("ERROR: .*");

            Stream<MatchResult> matches = scanner.findAll(pattern);

            matches.forEach(match -> System.out.println("Found error: " + match.group()));
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + e.getMessage());
        }
    }
}

In this example, the Scanner is used to parse a log file and find all lines that contain error messages.

Output (Assuming log.txt contains error lines):

Found error: ERROR: Connection lost
Found error: ERROR: Null pointer exception

Conclusion

The Scanner.findAll() method is useful for finding all occurrences of a pattern in the input and returning them as a stream of MatchResult objects. By understanding and using this method, you can efficiently search and process patterns in input data. 

Whether you are searching for specific words, patterns, or error messages, the findAll() method provides a reliable way to handle pattern matching in your Java applications.

Comments