findWithinHorizon() 是一個 Java Scanner 類方法,用於查找模式的下一次出現。 Java findWithinHorizon() 方法有兩種不同類型,可以根據其參數進行區分。這些是:
- Java Scanner findWithinHorizon(Pattern pattern, int horizon) 方法
- Java Scanner findWithinHorizon(String pattern, int horizon) 方法
findWithinHorizon(Pattern pattern, int horizon) 方法
這是 Java Scanner 類的內置方法,用於查找指定模式的下一次出現。此方法搜索輸入直到指定的搜索範圍,忽略分隔符。
findWithinHorizon(String pattern, int horizon) 方法
這是一個 Java Scanner 類方法,用於查找從指定字符串構造的模式的下一次出現,忽略分隔符。
用法
以下是 findWithinHorizon() 方法的聲明:
public String findWithinHorizon(String pattern, int horizon)
public String findWithinHorizon(Pattern pattern, int horizon)
參數
數據類型 | 參數 | 描述 | 必需/可選 |
---|---|---|---|
int | horizon | 這是搜索範圍 | Required |
String | pattern | 它是一個字符串,指定要搜索的模式 | Required |
Pattern | pattern | 這是要掃描的模式 | Required |
返回
findWithinHorizon() 方法返回與指定模式匹配的文本。
異常
IllegalStateException - 如果在掃描器關閉後調用完成,此方法會拋出異常。
IllegalArgumentException- 如果地平線為負,則拋出此異常。
兼容版本
Java 1.5 及以上
例子1
import java.util.Scanner;
import java.util.regex.Pattern;
public class ScannerFindWithinHorizonExample1 {
public static void main(String args[]){
String string = "Hello JavaTpoint! --> 1111, 2222, 3333";
Pattern pattern = Pattern.compile("[0-9]+");
Scanner scanner = new Scanner(string);
while (scanner.hasNext()) {
System.out.println("Output = " + scanner.findWithinHorizon(pattern, 26));
}
scanner.close();
}
}
輸出:
Output = 1111 Output = 2222 Output = 3333
例子2
import java.util.*;
public class ScannerFindWithinHorizonExample2 {
public static void main(String[] args) {
//Initialize scanner object
Scanner scan = new Scanner("x12 13 16 210");
//Declare horizon
int horizon = 8;
//Print the result
System.out.println("Print the string that satisifies the pattern within the horizon-");
System.out.println("Result:"+scan.findWithinHorizon("[0-9].*", horizon));
scan.close();
}
}
輸出:
Print the string that satisifies the pattern within the horizon- Result:12 13 1
例子3
import java.util.Scanner;
public class ScannerFindWithinHorizonExample3{
public static void main(String args[]){
String str = "Hello World! This is javaTpoint.";
//Create a new scanner Object
Scanner scanner = new Scanner(str);
//Print the result
System.out.println("Output:" + scanner.findWithinHorizon("[a-zA-Z].*", -16));
//Print the rest of the string
System.out.println("Left String:" + scanner.nextLine());
scanner.close();
}
}
輸出:
Exception in thread "main" java.lang.IllegalArgumentException:horizon < 0 at java.base/java.util.Scanner.findWithinHorizon(Scanner.java:1785) at java.base/java.util.Scanner.findWithinHorizon(Scanner.java:1746) at myPackage.ScannerFindWithinHorizonExample3.main(ScannerFindWithinHorizonExample3.java:9)
示例 4
import java.util.*;
import java.util.regex.Pattern;
public class ScannerFindWithinHorizonExample4{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
String userName;
System.out.print("Enter your email address:");
userName = scan.findWithinHorizon(Pattern.compile("[a-z]+"), 16);
System.out.println("Output:"+userName);
scan.close();
}
}
輸出:
Enter your email address:[email protected] Output:abhishek
相關用法
- Java Scanner findWithinHorizon()用法及代碼示例
- Java Scanner findInLine()用法及代碼示例
- Java Scanner reset()用法及代碼示例
- Java Scanner locale()用法及代碼示例
- Java Scanner nextByte()用法及代碼示例
- Java Scanner toString()用法及代碼示例
- Java Scanner useRadix()用法及代碼示例
- Java Scanner delimiter()用法及代碼示例
- Java Scanner nextLong()用法及代碼示例
- Java Scanner radix()用法及代碼示例
- Java Scanner remove()用法及代碼示例
- Java Scanner nextShort()用法及代碼示例
- Java Scanner nextBoolean()用法及代碼示例
- Java Scanner nextBigInteger()用法及代碼示例
- Java Scanner hasNext()用法及代碼示例
- Java Scanner skip()用法及代碼示例
- Java Scanner useDelimiter()用法及代碼示例
- Java Scanner nextInt()用法及代碼示例
- Java Scanner hasNextFloat()用法及代碼示例
- Java Scanner next()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Scanner findWithinHorizon() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。