开发者

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜