title: Java invalid entry counted
body: <p>I am trying to figure out how not to include invalid entries from being counted.
  I need to enter 5 scores and want "score count" to 5, but the code I made only enters 4 "score counts", including the invalid entry.  I am not required to enter the invalid entry and have no idea on how to exclude the invalid entries from being counted as scores.</p>
  
  <p>Here is the code below.</p>
  
  <pre><code>import java.util.Scanner;
  
   public class TestScoreApp
   {
     public static void main(String[] args)
     {
         // display operational messages
         System.out.println("Please enter test scores that range from 0 to 100.");
         System.out.println("To end the program enter 999.");
         System.out.println();  // print a blank line
  
         Scanner sc = new Scanner(System.in);
         String choice = "y";
  
         // get a series of test scores from the user
         while (!choice.equalsIgnoreCase("n"))
         {
         // initialize variables
         int scoreTotal = 0;
         int scoreCount = 0;
         int testScore = 0;
  
      System.out.println("Enter the number of test score to be entered: ");
      int numberOfTestScores = sc.nextInt();
  
             for (int i = 1; i &lt;= numberOfTestScores; i++)
             {
                  // get the input from the user
                  System.out.print("Enter score " + i + ": ");
                  testScore = sc.nextInt();
  
                  // accumulate score count and score total
                  if (testScore &lt;= 100)
                  {
                       scoreCount = scoreCount + 1;
                       scoreTotal = scoreTotal + testScore;
                  }
                  else if (testScore != 999)
                        System.out.println("Invalid entry, not counted");
                        sc.nextLine();
  
              // display the score count, score total, and average score
              }
              double averageScore = scoreTotal / scoreCount;
              String message = "\n" +
                   "Score count:   " + scoreCount + "\n"
                 + "Score total:   " + scoreTotal + "\n"
                 + "Average score: " + averageScore + "\n";
              System.out.println(message);
              System.out.println();
              System.out.println("Enter more test scores? (y/n)");
              choice= sc.next();
                   }
           }
   }
  </code></pre>
  
  <p>Here is an example of the file being run.</p>
  
  <blockquote>
    <p>Please enter test scores that range from 0 to 100. <br> To end the
    program enter 999. <br> <br> Enter the number of test score to be
    entered: <br> 5 <br> <br> Enter score 1: 66 <br> Enter score 2: 85
    <br> Enter score 3: 99 <br> Enter score 4: 79 <br> Enter score 5: 457
    <br> Invalid entry, not counted <br> <br></p>
    
    <p>Score count:   4 <br> Score total:   329 <br> Average score: 82.0 <br>
    <br> <br></p>
    
    <p>Enter more test scores? (y/n)</p>
  </blockquote>