title: How to run a continuous loop until a value in the correct range has been entered
body: <p>I need to Write a Java application which takes a number as input from the user and validates the input to ensure it is between 1 and 10. If the input is not in the correct range the application should
  use a loop to continue asking the user for input until a value in the correct range has been
  entered.</p>
  
  <p>Here's the code I have so far:</p>
  
  <pre><code>import java.util.Scanner;
  
  class Number {
  
      public static void main(String args[]) {
  
          int j = 0;
          int J = 0;
  
      Scanner input = new Scanner(System.in);
  
      System.out.println("Enter a number between 1 and 10: ");
          J = input.nextInt();
  
      while(J &gt; 10){
  
          j = input.nextInt();//read input from user
          System.out.println();
  
          if (j &gt;=1 &amp;&amp; j &lt;=10 ) {
              System.out.println("Validated input: " + j);
              input.close();          
          }//end if statement
  
          else {
              System.out.println("Enter a number between 1 and 10");
              j = input.nextInt();
          }//end else statement
  
      }//end while loop       
  
      }//end main method
  
  }//end class
  </code></pre>
  
  <p>I can get the code to work without the while loop but obviously it doesn't run continuously, with what I have done the question appears and you enter a number twice before the question appears again and there is an error with the validation message.</p>
  
  <p>So my question is: "How do I get the while loop to run correctly, I probably haven't it wrote correctly so could someone help me please"</p>
  
  <p>I'm using eclipse</p>