Assignemnt #125: Summing Three Numbers from Any File
Code
/// Name: Cassie Rapp
/// Period: 6
/// Program Name: Summing Three Numbers from Any File
/// File Name: FromAnyFile.java
/// Date Finished: 6/9/2016
import java.io.File;
import java.util.Scanner;
public class FromAnyFile {
public static void main( String[] args ) throws Exception {
Scanner keyboard = new Scanner(System.in);
String file;
int number1, number2, number3, sum;
System.out.print( "Which file would you like to read numbers from: " );
file = keyboard.next();
System.out.println( "Reading numbers from file \"" + file + "\"" );
System.out.println();
Scanner numbers = new Scanner( new File(file));
number1 = numbers.nextInt();
number2 = numbers.nextInt();
number3 = numbers.nextInt();
numbers.close();
sum = number1 + number2 + number3;
System.out.println( number1 + " + " + number2 + " + " + number3 + " = " + sum );
}
}
Picture of the output