Assignment #128
Code
/// Name: Jonathan Stine
/// Period: 5
/// Program Name: Summing Several Numbers from Any File
/// File Name: SummingSeveral.java
/// Date Finished: 5/4/2016
import java.io.File;
import java.util.Scanner;
public class SummingSeveral {
public static void main( String[] args ) throws Exception {
Scanner keyboard = new Scanner(System.in);
int sum = 0;
System.out.print( "\nWhich file would you like to read numbers from: " );
String file = keyboard.next();
System.out.println( "Reading numbers from \"" + file + "\"\n" );
Scanner reader = new Scanner( new File(file));
while(reader.hasNext()) {
int n = reader.nextInt();
sum+=n;
System.out.print( n + " " );
}
System.out.println( "\nTotal is " + sum );
}
}
Code Output