Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

AddNumbersFromFile BlueJ Project

Click here to download a BlueJ project for AddNumbersFromFile.


AddNumbersFromFile Source Code

The Java source code for AddNumbersFromFile is below. Click on a file name to download it.


AddNumbersFromFile.java

/*
 * Example AddNumbersFromFile: demonstration of reading
 * input from a file.  In this one, we assume there are
 * exactly 10 numbers in the file.
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: AddNumbersFromFile.java 2262 2013-11-22 00:27:28Z terescoj $
 */

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class AddNumbersFromFile {

    public static void main(String[] args) throws IOException {

        final int NUM_NUMBERS = 10;

        // construct a Scanner that will read from a file
        // instead of the keyboard.
        Scanner fromFile = new Scanner(new File("numbers"));

        // set up our running total for the sum of the numbers
        int sum = 0;

        // loop to read in NUM_NUMBERS numbers
        for (int i = 0; i < NUM_NUMBERS; i++) {
            // once we get here, there is no difference in how we use the Scanner
            // compared to what we did with keyboard Scanners.
            int number = fromFile.nextInt();
            sum += number;
        }
        
        // unlike the keyboard Scanners, we should close file Scanners
        fromFile.close();

        System.out.println("Sum of numbers: " + sum);
    }
}