import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;


public class Question3 {

	public static void main(String[] args) {
		
		// Instantiate File and Scanner objects
		File foobar, foobar2;
		Scanner input;
		PrintWriter output;
		
		// Set up file
		foobar = new File ("files\\Question3.txt");
		foobar2 = new File ("files\\Question3-output.txt");
		
		// Read file 
		try {
			
			// Set up Scanner and PrintWriter
			input = new Scanner(foobar);
			output = new PrintWriter(foobar2);
			
			// Set up variables
			String buffer;
			int lineTotal = 0, fullTotal = 0;
			
			// Process lines
			while (input.hasNextLine()) {
				
				buffer = input.nextLine(); // Current line
				Scanner inputLine = new Scanner(buffer); // Scanner for current line
				
				// Total up the integers
				while (inputLine.hasNextInt()) {
					lineTotal+=inputLine.nextInt();
				}
				
				// Print results
				System.out.println(buffer + ", total: " + lineTotal); // To console
				output.println(buffer + ", total: " + lineTotal); // To file
				fullTotal+=lineTotal; // File total 
				
				// Reset
				lineTotal = 0; // Reset line total
				inputLine.close(); // Close Scanner
				
			}
			
			// Clean up
			input.close();
			output.close();
			
			// Print file total
			System.out.println("File total: " + fullTotal);
		
		} catch (java.io.FileNotFoundException e) {
			
			// From "input = new Scanner(foobar);"
			System.err.println("Error: file " + foobar + " not found.");
			e.getMessage();
			System.exit(1);
		}

	}

}
