Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Programming Problem: This program should handle all of the RELEVANT excepti

ID: 3712500 • Letter: J

Question

JAVA Programming Problem: This program should handle all of the RELEVANT exceptions (including NumberFormatException, and IOException).

A method called openFile

Receives a string as its input argument,

Prints that string to console,

Receives a file name from the user, open that file,

Returns a BufferedInput object.

By handling relative exception(s), this method keeps asking for file names, printing appropriate error message, till user enters the correct file name (in our case input.txt).

A method called run, that:

Receives nothing, and has no return type.

Uses openFile method and looks at every line of the input.txt

Based on the below rules, writes into NovPayments.txt

Rules:In input.txt file, each line has this structure: firstName,lastName,hoursWorked,hourlyPayment

hoursWorked is an integer and hourlyPayment is a double (although both of them had to be converted to string to be stored in this file)

In output.txt file, each line has this structure: firstName,lastName,payment

payment = hoursWorked*hourlyPayment

Below, find a sample input.txt (left) and the corresponding NovPaments.txt (right). Test your program for the below input & output.

Minta,Lofton,$1200 Terrel,Metzer,$1490.4 Pouya,Rahmati,Error!!! Roxie,Galyean,$1008 Minta,Lofton,150,$8 Terrel,Metzer,162,$9.2 Pouya,Rahmati, 12F,$2.3 Roxie,Galyean,120,$8.4

Explanation / Answer

Payments.java

===========

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class Payments {

private BufferedReader openFile(String message){

System.out.println(message);

Scanner scanner = new Scanner(System.in);

BufferedReader br = null;

do{

String fileName = scanner.nextLine();

try {

br = new BufferedReader(new FileReader(Payments.class.getResource(fileName).getFile()));

} catch (Exception e1) {

System.out.println("File Not found. Please provide a existing file name.");

br = null;

}

}while(br==null);

return br;

}

private void run(){

try {

String path = Payments.class.getProtectionDomain().getCodeSource().getLocation().getPath();

BufferedWriter fw = new BufferedWriter(new FileWriter(path+"/NovPayments.txt"));

BufferedReader br = openFile("Enter File Name");

String input ="";

int error = 0;

int hours =-1;

double rate=-1;

double payment = -1;

while((input=br.readLine())!= null){

error=0;

String[]inputArray = input.split(",");

if(inputArray.length!=4){

continue;

}

try{

hours = Integer.parseInt(inputArray[2]);

}catch(Exception e){

error=1;

}

try{

rate = Double.parseDouble(inputArray[3].substring(1));

}catch(Exception e){

error = 1;

}

if(error == 0){

payment = hours*rate;

}

fw.write(inputArray[0]+","+inputArray[1]+","+(error==0?payment:"Error!!!"));

fw.newLine();

}

fw.close();

} catch (IOException e) {

System.out.println("Failed to open NovPayment.txt file and error="+e.getMessage());

}

}

public static void main(String[]args){

Payments obj = new Payments();

obj.run();

}

}

OUTPUT

========

Enter File Name
input.txt

Enter File Name
asd
File Not found. Please provide a existing file name.
input
File Not found. Please provide a existing file name.
input.txt

File Contents

==========

input.txt //in the same folder as the java file

======

Minta,Lofton,150,$8
Terrel,Metzer,162,$9.2,

Pouya,Rahmati,12F,$2.3
Roxie,Galyean,120,$8.4

NovPayments.txt // in the bin folder of your project folder

================

Minta,Lofton,1200.0
Terrel,Metzer,1490.3999999999999
Pouya,Rahmati,Error!!!
Roxie,Galyean,1008.0