我有以下代码来交互式地解析 System.in 以获取箭头按键:
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");
当我第一次按下右箭头时,一切顺利,但是当我第一次按下左箭头时,findWithinHorizon 会挂起等待更多输入,而它应该会失败并返回 null。
通过这种行为,我不知道如何使用这种技术来编写通用模式匹配解析器。
有什么线索吗?
谢谢,弗朗西斯科
最佳答案
findWithinHorizon
等待找到某些内容。你可以尝试一下这个模式
\\G\\033\\[C|\\G\\033\\[D
然后查看字符串内容。
关于java - findWithinHorizon 在应该失败时继续读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4235518/