title: validate a double using Scanner in java
body: <p>Okay, so I have looked through MANY validation scripts using <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html" rel="nofollow"><code>java.util.Scanner</code></a> and I can't find anything that would help me with what I need. I have gotten a good idea of how to set up my program, but I still need some help to get it to work the way I want it to. Basically, my goal is to ask the user to enter a height and I want to make sure it's not over 84 inches, numeric, and positive.</p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>// the part inside main() that is relevant
  double height = 0;
  Scanner input = new Scanner(System.in);
  
  height = get_height(height, input);
  
  private static double get_height(double height, Scanner input) {
          do {
              System.out.print("Please enter your height (in inches): ");
              while (!input.hasNextDouble() || input.nextDouble() &gt; 84) {
                  if (!input.hasNextDouble()) {
                      System.out.print("You must enter a valid number: ");
                      input.next();
                  }
                  else if (input.nextDouble() &gt; 84) {
                      System.out.print("Are you really taller than 7 feet? Try again: ");
                      input.next();
                  }
              }
              height = input.nextDouble();
          } while (height &lt;= 0);
  
          return height;
      }
  </code></pre>
  
  <p>These are the results I get:</p>
  
  <p><code>Please enter your height (in inches): hey<br>
    You must enter a valid number: 100<br>
    100<br>
    Are you really taller than 7 feet? Try again: 64<br>
    (blank space)<br>
    64<br>
    64</code></p>
  
  <p>As you can see, or maybe you can't tell, it doesn't quite hit the right messages it's supposed to and then it just leave's blank lines and you can enter data in twice before it will take (as you can see by the last 2 lines). I don't know why it's doing this but obviously it has something to do with my logic. I thought of using an if statement after the loop to validate that it's 7 feet, but if it's not valid then how do I restart the loop? My only idea for that would be to make a boolean variable named "valid" and initially set it to false and when it's true exit the loop and return. I could use some advice!!</p>
  
  <p>Oh, and this IS homework for those that are wondering. I don't exactly want my program written for me, but a suggestion would be lovely.</p>
  
  <p>EDIT: Okay, I got it myself. Thanks for the tons of help I received..</p>
  
  <pre><code>    private static double get_height(double height, Scanner input) {
      boolean valid = false;
      while (!valid) {
          do {
              System.out.print("Please enter your height (in inches): ");
              while (!input.hasNextDouble()) {
                  System.out.print("You must enter a valid number! Try again: ");
                  input.next();
              }
              height = input.nextDouble();
              if (height &gt; 84) {
                  System.out.println("Are you really over 7 feet? I don't think so..");
                  valid = false;
              }
               else {
                  valid = true;
              }
          } while (height &lt;= 0);
      }
  
      return height;
  }
  </code></pre>