Home »
Java programming language
Java Scanner findInLine() Method with Example
Scanner Class findInLine() method: Here, we are going to learn about the findInLine() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on March 26, 2020
Scanner Class findInLine() method
Syntax:
public String findInLine(Pattern patt);
public String findInLine(String patt);
- findInLine() method is available in java.util package.
- findInLine(Pattern patt) method is used to get the string that meets the given pattern (Pattern).
- findInLine(String patt) method is used to get the string that meets the given String (patt).
- These methods may throw an exception at the time of returning pattern.
IllegalStateException: This exception may throw when this Scanner is not opened.
- These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.
Parameter(s):
-
In the first case, findInLine(Pattern patt),
- Pattern patt – represents the pattern to find for.
-
In the second case, findInLine(String patt),
- String patt – represents the string denotes the pattern to find for.
Return value:
In both the cases, the return type of the method is String, it returns the message that meets the given pattern.
Example:
// Java program is to demonstrate the
// example of findInLine
import java.util.*;
import java.util.*;
import java.util.regex.*;
public class FindInLine {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using findInLine(Pattern) method
// is to find the next pattern from the given
// parameter ".Program"
String pattern = sc.findInLine(Pattern.compile(".Program"));
System.out.println("sc.findInLine(Pattern.compile(.Program)): " + pattern);
// By using findInLine(string) method
// is to find the next pattern from the given
// parameter of string "Java"
pattern = sc.findInLine("ava");
System.out.println("sc.findInLine(ava): " + pattern);
// Scanner closed
sc.close();
}
}
Output
sc.findInLine(Pattern.compile(.Program)): Program
sc.findInLine(ava): null