Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop in java until the users pushes enter

Tags:

java

How do I loop in java until the user pushes enter, then stop?

Something like

while(System.in != ""){
  do x;
}
like image 506
marscom Avatar asked Dec 07 '22 11:12

marscom


1 Answers

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);
}
like image 158
jrtc27 Avatar answered Dec 09 '22 02:12

jrtc27