A good answer might be:

  1. What is an advantage of this form of input file?
    • You don't have to count up how many integers are to be averaged.
  2. What is a disadvantage of this form of input file?
    • The integers must all be positive.

Problems with the Method

It often happens that a programmer will pick a sentinel value that will "never occur in the data." Of course, years later the program is used in a new situation where that special value does occur in the data and problems arise. Some of the "Year 2000" problems are this type of bug. Click here for an

Here is (nearly) the program that will work with this type of input data file:

import java.io.*;
class AddUpAllSentinel
{
  public static void main ( String[] args ) throws IOException
  {
    int value;     // the value of the current integer
    int sum = _____;   // initialize sum

    String line;
    BufferedReader stdin = new BufferedReader( 
        new InputStreamReader( System.in ) );

    // get first integer
    System.out.println("Enter first integer:");
    line   = stdin.readLine();
    value  = Integer.parseInt( line.trim() );

    int count = _______; // initialize count

    while ( value >= ________ )
    {
      sum    = _____________; // add to the sum
      count  = _____________;   // increment count

      System.out.println("Enter a number:");
      line   = stdin.readLine();
      value  = Integer.parseInt( line.trim() );
    }

    System.out.println( "Grand Total: " + sum );
  }
}

The program includes user prompts so that it is easy to debug with keyboard input. A "production" version of the program would change those lines into comments.

QUESTION 14:

Of course, you are eager to fill in those blanks.