findWithinHorizon keeps reading while it should fail
I've the following code to interactively parse System.in for arrow keypresses:
Scanner sc = new Scanner(System.in);
String str;
if ((str = sc.findWithinHorizon("\\G\\033\\[C", 0)) != null)
System.out.println("RIGHT ARROW");
else if ((str = sc.findWithinHorizon("\\G\\033\\[D", 0)) != null)
System.out.println("LEFT ARROW");
when I press first the right arrow all goes well开发者_如何学运维, but when I press first the left one, findWithinHorizon hangs waiting for more input while it should fail and return null.
With this behaviour, I don't see how to use this technique to program a general pattern-matching parser.
Any clues?
thanks, Francesc
findWithinHorizon
waits until it finds something. You could try the pattern
\\G\\033\\[C|\\G\\033\\[D
and then look at the strings content.
Problem is it doesn't scale. Perhaps a better solution is:
\\G\\033\\[C|\\G
instead of
\\G\033\\[C
making pattern always match and then compare lengths.
Any other alternatives?
精彩评论