title: Why is my Scanner skipping to the next line when my method is inside a for loop, but not when it is outside?
body: <p>I'm trying to get user input and store the info (last name, first name, score) in an array for an object. But after getting the last name, the console prints the prompt to input the first name and then skips to the next method for the score, not letting me input anything for the first name.</p>
  <p>However I tried moving the methods out of the for loop just to see if it worked and it did just fine. I feel that for some reason the Scanner is skipping to the next line while in the for loop.</p>
  <p>This is the code in my main class:</p>
  <pre><code>int studentsNumber = Console.getInt(&quot;Number of students: &quot;, 1, 500);
      Student std[] = new Student[studentsNumber];
      System.out.println(&quot;&quot;);
  
      for (int i = 0; i &lt; studentsNumber; i++) {
          System.out.println(
              &quot;STUDENT &quot; + (i + 1));
          String lastName = Console.getString(&quot;Last Name: &quot;);
          String firstName = Console.getString(&quot;First Name: &quot;);
          int score = Console.getInt(&quot;Score: &quot;, 0, 100);
          std[i] = new Student(firstName, lastName, score);
      }
  </code></pre>
  <p>The code on the Console class:</p>
  <pre><code>public class Console {
  private static Scanner sc = new Scanner(System.in);
  
  public static String getString(String prompt) {
      String x = &quot;&quot;;
      boolean isValid = false;
      while (!isValid) {
          System.out.println(prompt);
          if (sc.hasNextLine()) {
              if (sc.hasNextInt() || sc.hasNextDouble()) {
                  System.out.println(
                      &quot;Error! Invalid entry. Try again.&quot;);
                      sc.nextLine();
                  isValid = false;
              } else {
                  x = sc.nextLine();
                  isValid = true;
              }
          } else {
              System.out.println(
                  &quot;Error! This entry is required. Try again.&quot;);
              sc.nextLine();
              isValid = false;
          }
      }
      return x;
  }
  </code></pre>
  <p>Edit: getInt() method in the Console class:</p>
  <pre><code>public static int getInt(String prompt, int min, int max) {
      int x = 0;
      boolean isValid = false;
      while (!isValid) {
          System.out.println(prompt);
          if (sc.hasNextInt()) {
              x = sc.nextInt();
              isValid = true;
          } else {
              System.out.println(
                  &quot;Error! Invalid integer. Try again.&quot;);
              sc.nextLine();
              isValid = false;
          }
          if (x &lt; min) {
              System.out.println(
                  &quot;Error! Integer must be greater than &quot; + min + 
                  &quot; Try again.&quot;);
              sc.nextLine();
              isValid = false;
          } else if (x &gt; max) {
              System.out.println(
                  &quot;Error! Integer must be less than &quot; + max + &quot; Try again.&quot;);
              sc.nextLine();
              isValid = false;
          }
      }
      return x;
  }
  </code></pre>