Source of Sum3B_FromFile.java


  1: // Sum3B_FromFile.java
  2: //  read and sum three numbers from a file
  3: // revised from Sum3A_Interactive

  5: import java.util.Scanner;
  6: // added the next two lines
  7: import java.io.File;
  8: import java.io.FileNotFoundException;

 10: /**
 11:  * Reads three numbers from the file 3Nums.txt and prints their sum.
 12:  * @see Sum3A_FromUser.java
 13:  *
 14:  * @author Mark Young (A00000000)
 15:  */
 16: public class Sum3B_FromFile {

 18:     // added "throws" clause to main
 19:     public static void main(String[] args) 
 20:             throws FileNotFoundException {
 21:         // modified name of and argument to Scanner constructor
 22:         Scanner fin = new Scanner(new File("3Nums.txt"));
 23:         int n1, n2, n3;

 25:         // deleted prompt -- don't ask user for input -- it's from a file!
 26:         // System.out.println("\n\nEnter three numbers below: ");
 27:         // modified name of Scanner in the next three lines
 28:         n1 = fin.nextInt();
 29:         n2 = fin.nextInt();
 30:         n3 = fin.nextInt();
 31:         // deleted next line -- don't expect an Enter key in the file
 32:         // kbd.nextLine();

 34:         // print sum with echoed input
 35:         System.out.println("\nThe sum of " + n1 + ", " + n2 + ", and " + n3
 36:             + " is " + (n1 + n2 +n3) + ".\n");
 37:     }

 39: }