Java Utility Library

Java Scanner - findInLine() Method



The java.util.Scanner.findInLine() method attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.

Syntax

public String findInLine(String pattern)

Parameters

pattern Specify a string specifying the pattern to search for.

Return Value

Returns the text that matched the specified pattern.

Exception

Throws IllegalStateException, if the scanner is closed.

Example:

In the example below, the java.util.Scanner.findInLine() method returns next occurrence of the specified pattern constructed from the specified string, ignoring delimiters.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {

    //String to scan
    String MyString = "Hello Cello Hullo Hallo Jello";

    //creating a Scanner
    Scanner MyScan = new Scanner(MyString);

    //find and print word "Cello"
    System.out.println(MyScan.findInLine("Cello"));

    //prints the remaining portion of the line
    System.out.println(MyScan.nextLine()); 

    //close the scanner
    MyScan.close(); 
  }
}

The output of the above code will be:

Cello
 Hullo Hallo Jello

❮ Java.util - Scanner