Assignment #128 and Summing Several Numbers From Any File
Code
///Name: Tim Gibson
///Period: 6
///Project Name: Several Numbers
///File Name: Several.java
///Date Finished: 1/14/16
import java.util.Scanner;
import java.io.File;
public class Several {
public static void main ( String[] args ) throws Exception {
Scanner kb = new Scanner(System.in);
System.out.print( "What file would you like to read the numbers from? " );
String f = kb.next();
Scanner file = new Scanner( new File(f));
int a, s=0;
do {
a = file.nextInt();
System.out.print( a + " " );
s += a;
} while (file.hasNext());
System.out.println( "\nTotal is " + s );
}
}
Picture of the output