title: Problem With Scanner Mathod in Java: in.hasNext
body: <p>I have the following program below. I wanted it to only run if the input is a string, so I used <code>hasNext()</code> with an if statement.</p>
  
  <p>Unfortunately, it will still return true when I enter numbers. I want the program to execute the else statement if a letter grade is not entered. Is there a way around this? Thank you.</p>
  
  <pre><code>import java.util.Scanner;
  
  public class LetterGrade {
  
      public static void main(String[] args) {
  
          Scanner in = new Scanner(System.in);
  
          System.out.print("Enter a letter grade: ");
  
          if (in.hasNext()) {
              String letterGrade = in.next();
              System.out.println(letterGrade);
          } else {
              System.out.print("Not a valid letter grade");
          }
      }
  }
  </code></pre>