File I/O Part 1 #128 (SSN = Life)
Code
/// Name: Amir Salehi
/// Period: 7
/// Program Name: Summing Serveral Numbers
/// File Name:SSN.java
/// Date Finished: 5/29/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();
}
}
Picture(s) of the output