go to previous page   go to home page   go to next page        


  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 Sentinels

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 part A of the program that will work with this type of input data file. Of course, there are some blanks for you to fill.

import java.io.*;
import java.util.Scanner;

class TestGroupsSentinel
  public static void main ( String[] args ) throws IOException
    int value;     // the value of the current integer
    // Prompt for and open the input file   
    Scanner user = new Scanner( System.in );
    System.out.print("File name? ");
    String fileName = user.next().trim();
    Scanner scan = new Scanner( new File(fileName) );  

    // Group "A"
    int sumA = ;      // the sum of scores for group "A"
    int countA = ;    // initialize the group A count
    while ( (value = scan.nextInt()) != -1 )
      sumA   = sumA +  ; // add to the sum
      countA = countA + 1;    // increment the count

    if ( countA >  )
      System.out.println( "Group A average: " + ((double) sumA)/countA ); 
      System.out.println( "Group A  has no students" );


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