Java.util.Scanner.findInLine() Method



The java.util.Scanner.findInLine(Pattern pattern) 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.


Following is the declaration for java.util.Scanner.findInLine() method

public String findInLine(Pattern pattern)


  • pattern -- the pattern to scan for

Return Value

This method returns the text that matched the specified pattern.


  • IllegalStateException -- if this scanner is closed


The following example shows the usage of java.util.Scanner.findInLine() method.

package com.tutorialspoint;

import java.util.*;
import java.util.regex.Pattern;

public class ScannerDemo {

   public static void main(String[] args) {

   String s = "Hello World! 3+3.0=6";

   // create a new scanner with the specified String Object
   Scanner scanner = new Scanner(s);

   // find a pattern of any letter plus "ello"
   System.out.println("" + scanner.findInLine(Pattern.compile(".ello")));

   // print the next line of the string
   System.out.println("" + scanner.nextLine());

   // close the scanner

Let us compile and run the above program, this will produce the following result:

 World! 3+3.0=6