Near-complete Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

The program can add up however many integers there are in the file. The programmer does not need to know in advance how many that is. The the number can vary from file to file.

Near-complete Program

Here is the program, except for a blank or two:

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:

Fill in the blanks.