I am using the scanner class to capture user input from the command line (strings only), as an alternative to my previous question.
The following seems to work fine, except the blank lines are not caught as they should by the second conditional. For example when I press enter, this should be captured as a blank line and the second conditional should be true. However a new blank line is displayed on the console everytime, with the entire console "scrolling" upward if I keep hitting enter, rather than the logic in the conditional.
Is there a proper way to catch a blank input from the command line using scanner? (someone hitting just enter, or hit space a few times and then enter)
Thank you for any advice
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();
}
}