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:
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Geeksforgeeks has Scanner Class Methods" ;
System.out.println( "Target String:\n" + s);
Scanner scanner = new Scanner(s);
System.out.println( "\nAny 5 letter plus for : "
+ scanner.findInLine(
Pattern.compile( ".....for" )));
scanner.close();
}
catch (IllegalStateException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Target String:
Geeksforgeeks has Scanner Class Methods
Any 5 letter plus for : Geeksfor
Program 2: To demonstrate IllegalStateException
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Geeksforgeeks has Scanner Class Methods" ;
System.out.println( "Target String:\n" + s);
Scanner scanner = new Scanner(s);
scanner.close();
System.out.println( "\nAny 5 letter plus for : "
+ scanner.findInLine(
Pattern.compile( ".....for" )));
System.out.println( "" + scanner.nextLine());
}
catch (IllegalStateException e) {
System.out.println( "Exception thrown: " + e);
}
}
}
|
Output:
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:
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Geeksforgeeks has Scanner Class Methods" ;
System.out.println( "Target String:\n" + s);
Scanner scanner = new Scanner(s);
System.out.println( "\nAny 5 letter plus for : "
+ scanner.findInLine(
Pattern.compile( ".....for" )));
scanner.close();
}
catch (IllegalStateException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Target String:
Geeksforgeeks has Scanner Class Methods
Any 5 letter plus for : Geeksfor
Program 2: To demonstrate IllegalStateException
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Geeksforgeeks has Scanner Class Methods" ;
System.out.println( "Target String:\n" + s);
Scanner scanner = new Scanner(s);
scanner.close();
System.out.println( "\nAny 5 letter plus for : "
+ scanner.findInLine(
Pattern.compile( ".....for" )));
}
catch (IllegalStateException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
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)
Last Updated :
10 Oct, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...