I am trying to use and understand the Java Scanner#findWithinHorizon .
I wrote the following code snippet that uses this method, but I canβt understand how it works.
private static void findWithinHorizon() { String string = "Balkrishan Nagpal --> 1111, 2222, 3333"; Pattern pattern = Pattern.compile("[0-9]+"); Scanner scanner = new Scanner(string); while (scanner.hasNext()) { System.out.println("scanner.findWithinHorizon(pattern) = " + scanner.findWithinHorizon(pattern, 26)); } }
When I run the above method, I get the following output
scanner.findWithinHorizon(pattern) = 1111 scanner.findWithinHorizon(pattern) = 2222 scanner.findWithinHorizon(pattern) = 3333
but I expect the output will only contain
scanner.findWithinHorizon(pattern) = 1111
since I provided the horizon value as 26.
My understanding is that when looking for a matching result, the scanner will not go beyond index 26 in a row.
Can someone explain how this works?
source share