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

I am working in netbeans with Java and need some help with my OutputFile portion

ID: 3553639 • Letter: I

Question

I am working in netbeans with Java and need some help with my OutputFile portion of my code. In netbeans, the only thing that is giving me any form of error is the OutputFile portions which I have put in bold and underlined below.


This is what the program is meant to solve:


Write a program to get input from the user using dialog boxes. The program should repeatedly input 3 pieces of information: An employee's full name, their hourly rate of pay, and the total number of hours that they worked during the previous week. The program should write this information to an OutputFile object. When this program ends, you will have a text file that will have records with 3 fields of information in each record and would look something

"Bob Jones" 40.0 10.25

"Sam Smith" 45.5 15.00

"Jodi White" 32.25 20.50

etc.

Notice that the name has quotes around it (use the writeString( ) method to automatically put quotes around the employee's name). You can use any method you choose to stop the program. Be sure to close the OutputFile at the very end of the program or the data will not appear in the file.




package output.to.a.file.broc.east;


import javax.swing.JOptionPane;


public class OutputToAFileBrocEast

{


  

public static void main(String[] args)

{

OutputFile payFile;

payFile = new OutputFile ("payroll.txt");

String name;

String rate;

String hours;

String answer;

  

do

{

name = JOptionPane.showInputDialog ("Enter first and last name: ");

rate = JOptionPane.showInputDialog ("Enter hourly rate: ");

hours = JOptionPane.showInputDialog ("Enter hours for previous week: ");

  

payFile.writeWord(name);

payFile.writeWord(rate);

payFile.writeWord(hours);

payFile.writeEOL();

  

answer = JOptionPane.showInputDialog ("Do you have another employee? [y/n]");

  

}

while (answer.equalsIgnoreCase ("y"));

  

payFile.close();

}

  

}




Can someone please help me with what I am missing and point me in the write direction? I am new to Java and am at a loss.

Explanation / Answer

Why not you try the "BufferedWriter". I have written the code below. Have a look.


Edit:

Import these packages in your code. Use following lines just before your class declaration.

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;


---------------------------------------------------------------------------

try {

File file = new File("payroll.txt");


// if file doesnt exists, then create it

if (!file.exists()) {

file.createNewFile();

}


FileWriter fw = new FileWriter(file.getAbsoluteFile());

BufferedWriter bw = new BufferedWriter(fw);


String name;

String rate;

String hours;

String answer;


do {

name = JOptionPane.showInputDialog ("Enter first and last name: ");

rate = JOptionPane.showInputDialog ("Enter hourly rate: ");

hours = JOptionPane.showInputDialog ("Enter hours for previous week: ");


bw.write(name);

bw.write(rate);

bw.write(hours);

bw.write(" ");


answer = JOptionPane.showInputDialog ("Do you have another employee? [y/n]");

}

while (answer.equalsIgnoreCase ("y"));


bw.close();

} catch (IOException e) {

e.printStackTrace();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote