Assignment #128 and Summing Several Numbers
Code
/// Name: Lauren Baird
/// Period: 7
/// Program Name: Summing Several Numbers
/// File Name: SummingSeveralNumbers.java
/// Date Finished: 3/24/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);
String file;
int total = 0;
System.out.println();
System.out.print( "Which file would you like to read numbers from? " );
file = keyboard.next();
System.out.println( "Reading numbers from \"" + file + "\"... " );
System.out.println();
Scanner fileReader = new Scanner(new File(file));
while (fileReader.hasNext())
{
int x = fileReader.nextInt();
System.out.print( x + " " );
total = x + total;
}
System.out.println();
System.out.println( "Total is " + total );
fileReader.close();
}
}