Java Scanner - findWithinHorizon() Method
The java.util.Scanner.findWithinHorizon() method attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
Syntax
public String findWithinHorizon(String pattern, int horizon)
Parameters
pattern |
Specify a string specifying the pattern to search for. |
horizon |
Specify the search horizon. |
Return Value
Returns the text that matched the specified pattern.
Exception
- Throws IllegalStateException, if the scanner is closed.
- Throws IllegalArgumentException, if horizon is negative.
Example:
In the example below, the java.util.Scanner.findWithinHorizon() method returns next occurrence of the specified pattern constructed from the specified string, ignoring delimiters and within given horizon.
import java.util.*; public class MyClass { public static void main(String[] args) { //String to scan String MyString = "Hello Cello Hullo Hallo Jello"; //creating a Scanner Scanner MyScan = new Scanner(MyString); //find and print Hullo, with horizon 10 System.out.println(MyScan.findWithinHorizon("Hullo", 10)); //find and print Hullo, with horizon 20 System.out.println(MyScan.findWithinHorizon("Hullo", 20)); //prints the remaining portion of the line System.out.println(MyScan.nextLine()); //close the scanner MyScan.close(); } }
The output of the above code will be:
null Hullo Hallo Jello
❮ Java.util - Scanner