title: How can I add an error statement in a loop if the user entered letters instead of numbers
body: <p>How can I add an error statement in a loop if the user entered letters instead of numbers but the program should continue</p>
  <pre><code>package sample;
  import java.util.Scanner;
  import java.util.Random;
  
  public class Main {
  
      public static void main(String[] args) {
          Scanner scan = new Scanner(System.in);
          Random rand = new Random();
          int playerGuess;
          int randomNum = rand.nextInt(50) + 1;
          int numberOfAttempts = 0;
          while (true) {
              System.out.println(&quot;Guess a number from 1 to 50: &quot;);
              playerGuess = scan.nextInt();
              numberOfAttempts++;
  
              if (playerGuess == randomNum) {
                  System.out.println(&quot;Congratulations! The number was: &quot; + randomNum);
                  System.out.println(&quot;You got it in &quot; + numberOfAttempts + &quot; attempts.&quot;);
                  break;
              }
              else if (randomNum &gt; playerGuess) {
                  System.out.println(&quot;Your guess is too low. Try again.&quot;);
              }
              else {
                  System.out.println(&quot;Your guess is too high. Try again.&quot;);
              }
          }
      }
  }
  </code></pre>
  <p><strong>My program works fine but I forgot how to add an error statement in a loop. Can someone help me where and how to add an error statement when the user entered letters instead of numbers? Thank you in advance! :)))</strong></p>