← Back to Week 3 Hub
Workbook 3a — Exercise

Payroll Calculator (Write to File)

Take your payroll program further — instead of printing the report, write it to a file  |  Workbook p.23

The Exercise

Continue the payroll-calculator program you wrote earlier. Before, you read employee records from a file and printed the payroll report to the screen. Now you're going to save that report to a new file instead.

Ask the user two questions: the name of the input file (the employee file you already know how to read) and the name of the output file (where the payroll report should be written). Each line in the output file should follow this format: id|name|gross pay — for example 111|Cameron Tay|3277.65. When the program finishes, the user should be able to open the file they named and see the full report.

Bonus: If the user types a file name ending in .json, write a JSON array instead of pipe-delimited lines. Each employee becomes a JSON object like {"id":111,"name":"Cameron Tay","grossPay":3277.65}, and all objects together form one array.

Example Runs
Run 1 — Standard CSV output
Enter the input employee file: employees.csv
Enter the output payroll file: payroll-sept-2026.csv

Payroll report written to payroll-sept-2026.csv

--- payroll-sept-2026.csv ---
111|Cameron Tay|3277.65
142|Bossman Jones|4522.00
181|Dana Wyatt|2950.50
203|Priya Shah|3810.25
Run 2 — Bonus JSON output
Enter the input employee file: employees.csv
Enter the output payroll file: payroll-sept-2026.json

Payroll report written to payroll-sept-2026.json

--- payroll-sept-2026.json ---
[{"id":111,"name":"Cameron Tay","grossPay":3277.65},
 {"id":142,"name":"Bossman Jones","grossPay":4522.00},
 {"id":181,"name":"Dana Wyatt","grossPay":2950.50},
 {"id":203,"name":"Priya Shah","grossPay":3810.25}]
Concepts You'll Use
Flow
1. Prompt input file
2. Prompt output file
3. Detect extension
4. Open BufferedReader
5. Open FileWriter
6. Read & parse each line
7. Write formatted line
8. Close both streams

Workbook 3a, p.23 — Exercise: Payroll Calculator (Write)

← Payroll Calculator (Read) Format Dates →