title: java.util.Scanner: wait for input
body: <p>I use <code>java.util.Scanner</code> to read the commands from the console.</p>
  
  <pre><code>try
  {
      ICommand cmd = cmdReader.ReadCommand();
      cmd.Execute(conn);
  }
  catch(MyException ex)
  {
      // print a message about unknown command
      continue;
  }
  </code></pre>
  
  <p>Where <code>ReadCommand</code> is implemented as follows:</p>
  
  <pre><code>try (Scanner scanIn = new Scanner(System.in)) 
  {
      if (!scanIn.hasNextLine()) return null;
  
      line_ = scanIn.nextLine();
  
      ICommand command = parser_.ParseCommand(line_);
      return command;
  }
  </code></pre>
  
  <p>In the first iteration code works fine, I write something invalid (not a command), the code prints a warning and continues. But other iterations return <code>null</code> here <code>if (!scanIn.hasNextLine()) return null;</code> even if I write something in a console. Looks like <code>java.util.Scanner</code> doesn't see the input. Am I doing something wrong? And how then I can wait for the user input (don't want to use the cycle with <code>sleep</code>)?</p>