title: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?
body: <p>I am writing a simple program that prompts a user to enter a number of students, then asks the user to enter each student's name and score in order to determine which student has the highest score.</p>
  
  <p>I have written the program code and it compiles.  First line asks for a number of students and waits for input.  The second line is supposed to ask for a student name and wait for input, then a third line should print ans ask for that student's score, and wait for input but after the second line prints, the third line is immediately called (2nd line does not wait for input) and then I get a runtime error when trying to enter the requested information after the third line.</p>
  
  <p>How do I adjust the code so that the second line prints and waits for a string to be entered before printing the third line?</p>
  
  <pre><code>import java.util.Scanner;
  
  public class HighestScore {
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
  
          System.out.print("Enter the number of students: ");
          int numOfStudents = input.nextInt();
  
          System.out.print("Enter a student's name: ");
          String student1 = input.nextLine();
  
          System.out.print("Enter that student's score: ");
          int score1 = input.nextInt();
  
          for (int i = 0; i &lt;= numOfStudents - 1; i++) {
  
              System.out.println("Enter a student's name: ");
              String student = input.nextLine();
  
              System.out.println("Enter that student's score: ");
              int score = input.nextInt();
  
              if (score &gt; score1) {
              student1 = student;
              score1 = score;
              }
          }
          System.out.println("Top student " +
          student1 + "'s score is " + score1);
      }
  }
  </code></pre>