Java Scanner findInLine()方法及例子
findInLine(Pattern pattern)
java.util.Scanner 类的 findInLine(Pattern pattern) 方法试图找到指定模式的下一个出现点,而忽略分隔符。如果在下一个行的分隔符之前发现了该模式,扫描器将前进到匹配的输入,并返回与该模式匹配的字符串。如果在下一个分隔符之前的输入中没有检测到这样的模式,那么将返回null,扫描器的位置不变。
语法
public String findInLine(Pattern pattern)
参数: 该函数接受一个强制性参数Pattern,即扫描的模式。
返回值: 该函数返回扫描器的定界模式。
异常: 如果这个扫描器被关闭,该函数会抛出一个IllegalStateException。
下面的程序说明了上述函数。
程序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
程序2: 演示 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)
java.util.Scanner 类的 findInLine(String pattern) 方法试图找到由指定的字符串模式构建的模式的下一次出现,忽略了分隔符。
语法
public String findInLine(String pattern)
参数: 该函数接受一个被扫描的强制性参数字符串模式。
返回值: 该函数返回扫描仪的定界模式。
异常: 如果这个扫描器被关闭,该函数会抛出一个IllegalStateException。
下面的程序说明了上述函数。
程序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
程序2: 演示 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://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#findInLine(java.util.regex.Pattern)