title: While loop for re-prompt user after invalid input continues in java
body: <p>I want to re-prompt the user after a invalid input (empty or does not exist). The code below helps me do that <strong>but even if a valid input is given, it will still prompt the user for another input.</strong> How can I stop the loop from running again once a valid input is inserted?</p>
  
  <pre><code>    //get user input
      String year = JOptionPane.showInputDialog("Enter input");
  
      try{
          while (true) {
              List &lt;WebElement&gt; PageScrubberPagelet = dr.findElements(By.xpath("//li[@class='_3d5d']//a[contains(@data-key,'')]"));            
              for (WebElement el:PageScrubberPagelet) {
                  if (!(el.getAttribute("data-key")).contains(year) || year.equals("")) {
                      year = JOptionPane.showInputDialog(null, "Invalid input. Please re-enter (recent/ YYYY)", 
                          "Information", JOptionPane.INFORMATION_MESSAGE);
  
                  } else {
                      //find and click on the input year
                      WebElement yearButton = dr.findElement(By.xpath("//a[contains(@data-key,'" + year + "')]"));                        
                      yearButton.click(); 
                      break;
                  }
              }
          }
      } catch(Exception e){
          Logger.getLogger(ExpandFacebook.class.getName()).log(Level.SEVERE, "Invalid input!", e);
      } 
  </code></pre>
  
  <p>If the while loop is replace with if, it will only allow once for a invalid input and will not re-prompt the user for a third time.</p>