How do I loop in java until the user pushes enter, then stop?
Something like
while(System.in != ""){
do x;
}
Try the following:
while (System.in.available() == 0) {
// Do whatever you want
}
EDIT:
If you want to loop until the user presses enter without anything else, you will want something like the following (untested, but should be enough of a hint):
boolean blankLine = true;
loop:
while (true) {
int available;
while ((available = System.in.available()) == 0) {
// Do something
}
do {
switch (System.in.read()) {
default:
blankLine = false;
break;
case '\n':
if (blankLine)
break loop;
blankLine = true;
break;
}
} while (--available > 0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With