title: User input validation using While loop
body: <p>I am having trouble with this Java program validating user input using a while loop. I must use a while loop. The program works fine until the user enters a number that isn't valid which is when it prints <strong>Invalid number entered</strong> infinitely. Beginner level sorry, but Thank you for the help!</p>
  
  <pre><code>public class monthName {    
      public static void main(String[] args) {
          Scanner console = new Scanner(System.in);
          String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
                  "October", "November", "December" };
  
          // monthName[0]="January";
          // monthName[1]="February";
          // monthName[2]="March";
          // monthName[3]="April";
          // monthName[4]="May";
          // monthName[5]="June";
          // monthName[6]="July";
          // monthName[7]="August";
          // monthName[8]="September";
          // monthName[9]="October";
          // monthName[10]="November";
          // monthName[11]="December";
  
          int monthNumber = 0;
          System.out.println("Enter a month number: ");
              monthNumber = console.nextInt();
          while (monthNumber &gt; monthName.length || monthNumber &lt; 1) {
              System.out.println("Invalid number entered");
          }   
          System.out.println("The Month is: " + monthName[monthNumber - 1]);
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong>
  After switching the while loop of the code to this:
  It still does not give the month name</p>
  
  <pre><code>    int monthNumber = 0;
      System.out.println("Enter a month number: ");
      monthNumber = console.nextInt();
      while (monthNumber &gt; monthName.length || monthNumber &lt; 1) {
          System.out.println("Invalid number entered");
          System.out.println("Enter a month number: ");
          monthNumber = console.nextInt();
      }
      System.out.println("The month is: " + monthName);
  }
  </code></pre>
  
  <p>}</p>