Java中的扫描仪 findInLine() 方法及示例
findInLine(模式模式)
Java.util.Scanner类的findInLine(Pattern pattern)方法尝试查找指定模式忽略分隔符的下一个匹配项。如果在下一行分隔符之前找到该模式,则扫描程序会越过匹配的输入并返回与该模式匹配的字符串。如果在直到下一行分隔符的输入中没有检测到这样的模式,则返回 null 并且扫描仪的位置不变。
句法:
public String findInLine(Pattern pattern)
参数:该函数接受一个强制参数Pattern pattern ,它是被扫描的模式。
返回值:该函数返回扫描仪的分隔模式。
异常:如果此扫描仪关闭,该函数将引发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);
// 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
方案二:演示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(字符串模式)
java.util.Scanner类的findInLine(String pattern)方法尝试查找从指定字符串模式构造的模式的下一个匹配项,忽略分隔符。
句法:
public String findInLine(String pattern)
参数:该函数接受扫描的强制参数字符串模式。
返回值:该函数返回扫描仪的分隔模式。
异常:如果此扫描仪关闭,该函数将引发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);
// 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
方案二:演示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
参考: https: Java Java.util.regex.Pattern)