Scanner findInLine() method in Java with Examples
findInLine(Pattern pattern)
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
findInLine(String pattern)
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)
Recommended Posts:
- Scanner hasNextLine() method in Java with Examples
- Scanner hasNextFloat() method in Java with Examples
- Scanner hasNextDouble() method in Java with Examples
- Scanner hasNextBoolean() method in Java with Examples
- Scanner nextShort() method in Java with Examples
- Scanner useDelimiter() method in Java with Examples
- Scanner ioException() method in Java with Examples
- Scanner nextBigInteger() method in Java with Examples
- Scanner hasNextShort() method in Java with Examples
- Scanner nextByte() method in Java with Examples
- Scanner radix() method in Java with Examples
- Scanner reset() method in Java with Examples
- Scanner close() method in Java with Examples
- Scanner useRadix() method in Java with Examples
- Scanner toString() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



