Scanner findInLine() method in Java with Examples - GeeksforGeeks

10 Oct, 2018
The findInLine(Pattern pattern) method of java.util.Scanner class tries 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.
Syntax:
public String findInLine(Pattern pattern)
Parameters: The function accepts a mandatory parameter Pattern pattern which is pattern which is scanned for.
Return Value: The function returns the scanner’s delimiting pattern.
Exceptions: The function throws an IllegalStateException if this scanner is closed.
Below programs illustrate the above function:
Program 1:
// Java program to illustrate the// findInLine() method of Scanner class in Java  import java.util.*;import java.util.regex.Pattern;  public class GFG1 {    public static void main(String[] argv)        throws Exception    {          try {              // Get the string to be searched            String s = "Geeksforgeeks has Scanner Class Methods";              // Print the string            System.out.println("Target String:\n" + s);              // create a new scanner            // with the specified String Object            Scanner scanner = new Scanner(s);              // finds a pattern of any 5 letter plus "for"            System.out.println("\nAny 5 letter plus for : "                               + scanner.findInLine(                                     Pattern.compile(".....for")));              // close the scanner            scanner.close();        }        catch (IllegalStateException e) {            System.out.println("Exception thrown : " + e);        }    }}
Target String:
Geeksforgeeks has Scanner Class Methods

Any 5 letter plus for : Geeksfor

Program 2: To demonstrate IllegalStateException
// Java program to illustrate the// findInLine() method of Scanner class in Java  import java.util.*;import java.util.regex.Pattern;  public class GFG1 {    public static void main(String[] argv)        throws Exception    {          try {              // Get the string to be searched            String s = "Geeksforgeeks has Scanner Class Methods";              // Print the string            System.out.println("Target String:\n" + s);              // create a new scanner            // with the specified String Object            Scanner scanner = new Scanner(s);              // close the scanner            scanner.close();              // finds a pattern of any 5 letter plus "for"            System.out.println("\nAny 5 letter plus for : "                               + scanner.findInLine(                                     Pattern.compile(".....for")));              // print the next line of the string            System.out.println("" + scanner.nextLine());        }        catch (IllegalStateException e) {            System.out.println("Exception thrown: " + e);        }    }}
Target String:
Geeksforgeeks has Scanner Class Methods
Exception thrown:
 java.lang.IllegalStateException:
 Scanner closed

The findInLine(String pattern) method ofjava.util.Scanner class tries to find the next occurrence of a pattern constructed from the specified string pattern, ignoring the delimiters.
Syntax:
public String findInLine(String pattern)
Parameters: The function accepts a mandatory parameter string pattern which is scanned for.
Return Value: The function returns the scanner’s delimiting pattern.
Exceptions: The function throws an IllegalStateException if this scanner is closed.
Below programs illustrate the above function:
Program 1:
// Java program to illustrate the// findInLine() method of Scanner class in Java  import java.util.*;import java.util.regex.Pattern;  public class GFG1 {    public static void main(String[] argv)        throws Exception    {          try {              // Get the string to be searched            String s = "Geeksforgeeks has Scanner Class Methods";              // Print the string            System.out.println("Target String:\n" + s);              // create a new scanner            // with the specified String Object            Scanner scanner = new Scanner(s);              // finds a pattern of any 5 letter plus "for"            System.out.println("\nAny 5 letter plus for : "                               + scanner.findInLine(                                     Pattern.compile(".....for")));              // close the scanner            scanner.close();        }          catch (IllegalStateException e) {            System.out.println("Exception thrown : " + e);        }    }}
Target String:
Geeksforgeeks has Scanner Class Methods

Any 5 letter plus for : Geeksfor

Program 2: To demonstrate IllegalStateException
// Java program to illustrate the// findInLine() method of Scanner class in Java  import java.util.*;import java.util.regex.Pattern;  public class GFG1 {    public static void main(String[] argv)        throws Exception    {          try {              // Get the string to be searched            String s = "Geeksforgeeks has Scanner Class Methods";              // Print the string            System.out.println("Target String:\n" + s);              // create a new scanner            // with the specified String Object            Scanner scanner = new Scanner(s);              // close the scanner            scanner.close();              // finds a pattern of any 5 letter plus "for"            System.out.println("\nAny 5 letter plus for : "                               + scanner.findInLine(                                     Pattern.compile(".....for")));        }          catch (IllegalStateException e) {            System.out.println("Exception thrown : " + e);        }    }}
Target String:
Geeksforgeeks has Scanner Class Methods
Exception thrown :
 java.lang.IllegalStateException:
 Scanner closed

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#findInLine(java.util.regex.Pattern)
Java - util package
Java-Functions
Java-I/O
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Exceptions in Java
Constructors in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
PriorityQueue in Java
How to remove an element from ArrayList in Java?