findInLine(Pattern pattern)
java.util.Scanner類的findInLine(Pattern pattern)方法嘗試忽略分隔符來查找指定模式的下一個匹配項。如果在下一個行分隔符之前找到該模式,則掃描程序將前進經過匹配的輸入並返回與該模式匹配的字符串。如果直到下一個行分隔符的輸入中都未檢測到這種模式,則返回null,並且掃描儀的位置不變。
用法:
public String findInLine(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)
相關用法
- Java Scanner nextInt()用法及代碼示例
- Java Scanner hasNextInt()用法及代碼示例
- Java Scanner hasNextByte()用法及代碼示例
- Java Scanner nextByte()用法及代碼示例
- Java Scanner hasNextBigInteger()用法及代碼示例
- Java Scanner hasNextDouble()用法及代碼示例
- Java Scanner hasNextBoolean()用法及代碼示例
- Java Scanner hasNextBigDecimal()用法及代碼示例
- Java Scanner nextBigInteger()用法及代碼示例
- Java Scanner locale()用法及代碼示例
- Java Scanner nextLine()用法及代碼示例
- Java Scanner ioException()用法及代碼示例
- Java Scanner nextBigDecimal()用法及代碼示例
- Java Scanner nextBoolean()用法及代碼示例
- Java Scanner hasNextShort()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Scanner findInLine() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。