public class TransactionReader
1: import java.io.FileInputStream;
2: import java.io.FileNotFoundException;
3: import java.io.IOException;
4: import java.io.File;
5: import java.util.Scanner;
7: public class TransactionReader
8: {
9: public static void main(String[] args)
10: {
11: String fileName = "Transactions.txt";
12: try
13: {
14: Scanner inputStream = new Scanner(new File(fileName));
15: // Read the header line
16: String line = inputStream.nextLine();
17: // Total sales
18: double total = 0;
19: // Read the rest of the file line by line
20: while (inputStream.hasNextLine())
21: {
22: // Contains SKU,Quantity,Price,Description
23: line = inputStream.nextLine();
24: // Turn the string into an array of strings
25: String[] ary = line.split(",");
26: // Extract each item
27: String SKU = ary[0];
28: int quantity = Integer.parseInt(ary[1]);
29: double price = Double.parseDouble(ary[2]);
30: String description = ary[3];
31: // Output item
32: System.out.printf("Sold %d of %s (SKU: %s) at $%1.2f each.\n",
33: quantity, description, SKU, price);
34: // Compute total
35: total += quantity * price;
36: }
37: System.out.printf("Total sales: $%1.2f\n",total);
38: inputStream.close( );
39: }
40: catch(FileNotFoundException e)
41: {
42: System.out.println("Cannot find file " + fileName);
43: }
44: catch(IOException e)
45: {
46: System.out.println("Problem with input from file " + fileName);
47: }
48: }
49: }