Computer Science 202
Introduction to Programming
Fall 2013, The College of Saint Rose
AddNumbersFromFileSentinel BlueJ Project
Click here to download a BlueJ project for AddNumbersFromFileSentinel.
AddNumbersFromFileSentinel Source Code
The Java source code for AddNumbersFromFileSentinel is below. Click on a file name to download it.
/* * Example AddNumbersFromFileSentinel: read in a sequence of numbers * from a file, adding them up, until the sentinel value of 0 is read. * * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012 * * $Id: AddNumbersFromFileSentinel.java 2262 2013-11-22 00:27:28Z terescoj $ */ import java.io.File; import java.io.IOException; import java.util.Scanner; public class AddNumbersFromFileSentinel { 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; // variable for the next number to add int number = 0; // loop to read in numbers until 0 is encountered do { // once we get here, there is no difference in how we use the Scanner // compared to what we did with keyboard Scanners. number = fromFile.nextInt(); sum += number; } while (number != 0); // unlike the keyboard Scanners, we should close file Scanners fromFile.close(); System.out.println("Sum of numbers: " + sum); } }