Java Scanner - findInLine() Method
The java.util.Scanner.findInLine() method attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.
Since this method continues to search through the input looking for the specified pattern, it may buffer all of the input searching for the desired token if no line separators are present.
Syntax
public String findInLine(Pattern pattern)
Parameters
pattern |
Specify the pattern to scan 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 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); while(MyScan.hasNext()) { //print five word pattern that ends with ello System.out.println(MyScan.findInLine(".ello")); } //close the scanner MyScan.close(); } }
The output of the above code will be:
Hello Cello Jello
❮ Java.util - Scanner