title: Java validate only values between 1 and 3
body: <p>I am trying to only accept integers between the values of 1 and 3 using a while loop and nested if statement in Java.</p>
  
  <p>Anything outside of this range, produces an error message</p>
  
  <p>The program should only accept integers between 1 and 3, any strings of text or decimal values should also produce the same error message and loop back to the original print statement (enter a number: ) </p>
  
  <p>The code below runs without any compiler errors although the statement || (a &lt; 1 || a > 3)) will always produce the error message, regardless of the value.</p>
  
  <p>If I was to delete this statement, the program will run and only accept integers of any value, (error message appearing when a string or decimal value is entered)</p>
  
  <p>Could anyone help range this program, only accepting values of between 1 and 3, thanks. </p>
  
  <p>import java.util.Scanner;</p>
  
  <p>public class Validate {
      public static void main(String[] args) {</p>
  
  <pre><code>Scanner input = new Scanner(System.in);
  
  int a = 0;
  
  while (true) {
      System.out.print("enter a number: ");
  
  
      if (!input.hasNextInt() || !input.hasNext() || (a &lt; 1 || a &gt; 3)) {
  
          System.out.println("Oops! "); 
  
          input.nextLine();
      }
  
  
      else { 
          a = input.nextInt();
          break;
      }
  }
  input.close();
  System.out.println("a = " + a);
  
  
  }
  </code></pre>
  
  <p>}</p>