COMP2243 - Programming and Problem Solving

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Repetitions/Loops WhileLoop.java LoopWithCounter.java LoopWithSentinel.java LoopWithFlag.java SimpleInterest.java Factorial.java Fibnonacci.java PrimeNumber.java GCD.java PopulationGrowth.java ForLoop.java DoWhileLoop.java FileIO_Tickets.java FileIO_Salary.java NestedLoop.java



FileIO_Salary Program


Description

This program reads data from the data file FileIO_Salary_Data.txt and then calculates the new salary based on the raise percentage for each employee.

It also calculates the average current and new salary of all employees.

The results are saved to another file FileIO_Salary_Result.txt.

The data file contains the employee salary information.



The top line is not data, just the caption.

The first column is the employee name, the second column is the current salary, and the third column is the raise percentage.

Associated Concepts:

File Input and Output

File I/O Exception

Algorithm:

Any exceptions related file I/O are checked exceptions.

It is required to handle them.

Make sure add throws IOException clause in the main method signature

public static void main(String [] args) throws IOException {

Create a File object and associate it with the data file and
then create a Scanner object and associate it with the File object

Create a FileWriter object and associate it with the output file and
then create a PrintWriter object and associate it with the FileWriter object

Read the first line (caption) and discard it

Use a while to check the end of file indicator (as long as the file has data item):

    Read data items one at a time

    Calculate the new salary

    Add current salary to total salary

    Add new salary to total new salary

    Increment count by 1

    Remove _ from the name

    Save the individual data items to output file

Once the loop is ended, calculate the averages and save them to the output file.

Close both file input and output objects

Source Code



Sample Run




Output File FileIO_Salary_Result.txt:



Video