我正在使用扫描仪类从命令行捕获用户输入(仅限字符串),作为我之前问题的替代方法。
以下似乎工作正常,除了空白行没有被第二个条件捕获。例如,当我按下回车键时,这应该被捕获为一个空行,并且第二个条件应该为真。但是,每次都会在控制台上显示一个新的空白行,如果我继续按 Enter 键,整个控制台会向上“滚动”,而不是条件中的逻辑。
是否有正确的方法可以使用扫描仪从命令行捕获空白输入?(有人按进入,或按空间几次然后进入)
谢谢你的任何建议
Machine aMachine = new Machine();
String select;
Scanner br = new Scanner(System.in);
while(aMachine.stillInUse()){
select = br.next();
if (Pattern.matches("[rqRQ1-6]", select.trim())) {
aMachine.getCommand(select.trim().toUpperCase()).execute(aMachine);
}
/*
* Ignore blank input lines and simply
* redisplay current status -- Scanner doesn't catch this
*/
else if(select.trim().isEmpty()){
aMachine.getStatus();
/*
* Everything else is treated
* as an invalid command
*/
else {
System.out.println(aMachine.badCommand()+select);
aMachine.getStatus();
}
}