Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

AddNumbersFromFileAll BlueJ Project

Click here to download a BlueJ project for AddNumbersFromFileAll.

AddNumbersFromFileAll Source Code

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

 * Example AddNumbersFromFileAll: read in and add up all of the numbers in
 * a file.  Continue until there are no more numbers to be read
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 * $Id: 1967 2012-11-01 03:23:14Z terescoj $
import java.util.Scanner;

public class AddNumbersFromFileAll {

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

        // 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 numbers as long as more numbers are available on the input -
        // the hasNextInt method will tell us if there is another int value available
        // to be read by the Scanner
        while (fromFile.hasNextInt()) {
            // 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

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