Question #206966

Amend or edit given code PaySlip.java so that if the input file does not exist, the program handles the FileNotFoundException, outputs an appropriate message, and terminates normally.


Expert's answer

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

public class PaySlip {
    public void readFile(String fileName){
        try(Scanner in = new Scanner(new File(fileName))){
            while(in.hasNextLine()){
                in.nextLine();
            }
        }catch (FileNotFoundException e){
            System.out.println(e.getMessage());
            System.exit(0);
        }
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS