Java Scanner findInLine() Method

The findInLine() method in Java, part of the java.util.Scanner class, is used to search for a pattern within the current line of the input. It can return either a String representing the matched input or a MatchResult object for more detailed match information.

Table of Contents

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

Introduction

The findInLine() method searches the current line of the input for a specified pattern. This method is useful when you need to match patterns within a single line without advancing the scanner to the next line.

findInLine() Method Syntax

There are two overloaded versions of the findInLine() method:

Using a String Pattern

public String findInLine(String pattern)

Using a Pattern Object

public String findInLine(Pattern pattern)

Parameters:

  • pattern: A String or Pattern representing the regular expression to be matched.

Returns:

  • A String representing the matched input, or null if no match is found.

Throws:

  • IllegalStateException: If the scanner is closed.

Understanding findInLine()

The findInLine() method searches the current line of the input for a match of the specified pattern. If a match is found, it returns the matched string. If no match is found, it returns null. This method does not advance the scanner past the current line.

Examples

Basic Usage

To demonstrate the basic usage of findInLine(), we will create a Scanner object, use it to search for a pattern within the current line, and print the matched string.

Example

import java.util.Scanner;

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

        String match = scanner.findInLine("banana");

        if (match != null) {
            System.out.println("Found: " + match);
        } else {
            System.out.println("No match found");
        }

        scanner.close();
    }
}

Output:

Found: banana

Using a Custom Pattern

This example shows how to use findInLine() with a custom pattern to find a word starting with a specific letter.

Example

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

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

        String match = scanner.findInLine(pattern);

        if (match != null) {
            System.out.println("Found: " + match);
        } else {
            System.out.println("No match found");
        }

        scanner.close();
    }
}

Output:

Found: Bob

Real-World Use Case

Parsing Command Line Input

In real-world applications, the findInLine() method can be used to parse command line inputs to find specific commands or options within a single line.

Example

import java.util.Scanner;

public class CommandLineParser {
    public static void main(String[] args) {
        String command = "copy --source=file.txt --destination=/backup";
        Scanner scanner = new Scanner(command);

        String sourceOption = scanner.findInLine("--source=\\S+");
        String destinationOption = scanner.findInLine("--destination=\\S+");

        if (sourceOption != null) {
            System.out.println("Source option: " + sourceOption);
        } else {
            System.out.println("Source option not found");
        }

        if (destinationOption != null) {
            System.out.println("Destination option: " + destinationOption);
        } else {
            System.out.println("Destination option not found");
        }

        scanner.close();
    }
}

Output:

Source option: --source=file.txt
Destination option: --destination=/backup

In this example, the Scanner is used to find specific command line options within a single line of input.

Conclusion

The Scanner.findInLine() method is useful for searching for patterns within the current line of the input. By understanding and using this method, you can efficiently match and process patterns in single-line inputs. Whether you are searching for specific words, patterns, or command line options, the findInLine() method provides a reliable way to handle pattern matching in your Java applications.

Comments