title: Problems looping in a while loop with nested if statements
body: <p>I'm having troubles with the output of my program. I've developed the class 'GetInput' as a constructor which I can reuse when asking various questions for input. Each question asked will need to be equal to or more than a minimum/less then a maximum number which is passed on to the class/constructor. The problem I have is that when the while loop is run, it asked for input four times before finally returning the correct value.  </p>
  
  <p>I've added flags which I've worked out when they display. The first shows after input is added the first time. Then the second time, then the fourth time. The fourth time it also displays the flag 'the end' which I want it to reach in one iteration. <strong>Why is it looping four times before finally correctly returning the value?</strong></p>
  
  <p>Thanks so much in advance. This is only my second day learning java and this is driving me insane.</p>
  
  <pre><code>import java.util.Scanner; //Import the scanner class
  
  public class main {
  public static void main(String[] args) {
  
      //Set variables to hold the entry cost for each category
      int costAccChild = 2;
      int costUnaccChild = 5;
      int costAdult = 10;
      int costSenior = 8;
  
      int test = GetInput(0, 1, "Enter a group? (Yes=1/No=0)");
      System.out.println(test);
      System.out.println("the end");
  
  }
  
   static int GetInput(int min, int max, String request){     
      boolean inputValid = false; //Sets default value to variable for while loop
      int userInput = 0; //Sets default variable for input return
  
      while (inputValid == false) { //Loops until receives correct input
          System.out.println(request); //Prints question asking for input
          Scanner inputFromUser = new Scanner(System.in); //Ask user for input
          System.out.print("First time"); //FLAG DISPLAYS AFTER FIRST SCANNER
  
          if (inputFromUser.hasNextInt() == true){ //Check if input has an integer
  
              System.out.print("Second Time"); //FLAG SHOWS AFTER SECOND SCANNER
  
              if (inputFromUser.nextInt() &gt;= min &amp;&amp; inputFromUser.nextInt() &lt;= max ){ //Check if input is valid
                  userInput = inputFromUser.nextInt();
                  inputValid= true;
  
                  System.out.print("Fourth time"); //FLAG WORKS FORTH TIME
  
              }else{ //Input is not correct integer, give error message
                  System.out.println("Input is not valid");           
                  }   
  
          }else{ //Input is not an integer at all, give error message
              System.out.println("Input is not valid");
          }
      }
      return userInput; //Returns valid input
      }
  }
  </code></pre>