Assignment #128 SummingSeveralNumbers
Code
/// Name: Lacey Reese
/// Period: 5
/// Program Name: SummingSeveralNumbers
/// File Name: SummingSeveralNumbers.java
/// Date Finished 5/27/2016
import java.io.File;
import java.util.Scanner;
public class SummingSeveralNumbers
{
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 );
}
}
Picture of the output
//