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

Answer:

The number of integers to be added is not fixed by the program. The data itself says how many integers are to be added.


Near-complete Program

Here is the program, except for a blank or two. Think before you click them.

import java.io.*;
import java.util.Scanner;
class AddUpAll
{
  public static void main ( String[] args ) throws IOException
  {
    int value;          // the value of the current integer
    int limit;          // the number of integers to add up
    int sum = ______; // initialize sum
    
    // 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) );  // Note how this is done.

    // get the number of integers to add up
    limit = scan.nextInt();
    int count = ______;   // initialize count

    while ( count <= ______ )
    {
      value  = scan.nextInt();
      sum    = ______;   // add to the sum
      count  = ______;   // increment count
    }

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

QUESTION 4:

Why is user input trimmed? user.next().trim()