Assignment #128 and Summing Several Numbers
Code
/// Name: Ali Kurland
/// Period: 6
/// Program Name: Summing Several Numbers from Any File
/// File Name: SumSeveral.java
/// Date Finished: 5/17/2016
import java.io.File;
import java.util.Scanner;
public class SumSeveral {
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
String choice;
int total=0;
System.out.print( "Which file would you like to read numbers from: " );
choice = keyboard.next();
System.out.println( "Reading numbers from " + "\"" + choice + "\"\n" );
Scanner reader = new Scanner(new File(choice));
while(reader.hasNext()) {
int n = reader.nextInt();
total = total + n;
System.out.print( n + " " );
}
System.out.println( "\nTotal is " + total );
reader.close();
}
}
Picture of the output