Java Scanner findWithinHorizon() MethodThe findWithinHorizon() is a Java Scanner class method which is used to find the next occurance of a pattern. There is two different types of Java findWithinHorizon() method which can be differentiated depending on its parameter. These are:
findWithinHorizon(Pattern pattern, int horizon) MethodThis is an inbuilt method of Java Scanner class which is used to find the next occurrence of the specified pattern. This method searches through the input up to the specified search horizon, ignoring delimiters. findWithinHorizon(String pattern, int horizon) MethodThis is a Java Scanner class method which is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters. SyntaxFollowing is the declaration of findWithinHorizon() method: Parameter
ReturnsThe findWithinHorizon() method returns the text that matched the specified pattern. ExceptionsIllegalStateException- This method throws exception if the innvocation is done after the scanner has been closed. IllegalArgumentException- It thrown this exception if horizon is negative. Compatibility VersionJava 1.5 and above Example 1Output: Output = 1111 Output = 2222 Output = 3333 Example 2Output: Print the string that satisifies the pattern within the horizon- Result: 12 13 1 Example 3Output: 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) Example 4Output: Enter your email address: [email protected] Output:abhishek Next TopicJava-scanner-hasnextbigdecimal-method |