Hi. I'm supposed to write a program that will write to a file called payroll.dat. So far I haven't been able to get it to work. I keep getting an error that says "invalid number" and nothing is being written to the file. I sent it to my instructor and he sent it back with notes. I'll post my script and then I'll post my instructors notes. Hopefully someone can tell me how to make this work.
package assignment_10;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import assignment8.string;
public class CreateMailOrderFile extends JFrame implements ActionListener, WindowListener {
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
JPanel titlePanel = new JPanel();
JPanel displayPanel = new JPanel(new GridLayout(6, 1));
JPanel dPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel buttonPanel = new JPanel();
private JLabel companyName = new JLabel("Payroll Inc.");
Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
private JLabel prompt = new JLabel("Enter Order Information");
private JTextField employeename = new JTextField(20);
private JTextField employeenumber = new JTextField(10);
private JTextField hourlyrate = new JTextField(5);
private JTextField regularhours = new JTextField(2);
private JTextField overtimehours = new JTextField(2);
private JLabel nLabel = new JLabel("Employee Name");
private JLabel iLabel = new JLabel("Employee Number");
private JLabel qLabel = new JLabel("Hourly Rate ");
private JLabel rLabel = new JLabel("Regular Hours ");
private JLabel oLabel = new JLabel("Overtime Hourse");
private JButton enterDataButton = new JButton("Enter data");
DataOutputStream ostream;
public CreateMailOrderFile() {
super("Create Order File - Assignment 10");
setSize(WIDTH, HEIGHT);
try {
ostream = new DataOutputStream(new FileOutputStream("payroll.dat"));
} catch (IOException e) {
System.err.println("File not opened");
System.exit(1);
}
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
companyName.setFont(bigFont);
titlePanel.add(companyName);
titlePanel.setBackground(Color.white);
dPanel1.add(prompt);
displayPanel.add(dPanel1);
dPanel2.add(nLabel);
dPanel2.add(employeename);
displayPanel.add(dPanel2);
dPanel3.add(iLabel);
dPanel3.add(employeenumber);
displayPanel.add(dPanel3);
dPanel4.add(qLabel);
dPanel4.add(hourlyrate);
displayPanel.add(dPanel4);
dPanel5.add(rLabel);
dPanel5.add(regularhours);
displayPanel.add(dPanel5);
dPanel6.add(oLabel);
dPanel6.add(overtimehours);
displayPanel.add(dPanel6);
buttonPanel.setBackground(Color.white);
buttonPanel.setLayout(new FlowLayout());
enterDataButton.setMnemonic(KeyEvent.VK_E);
buttonPanel.add(enterDataButton);
enterDataButton.addActionListener(this);
contentPane.add(titlePanel, BorderLayout.NORTH);
contentPane.add(displayPanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(this);
}
private void writeRecord() {
int empNum, regularNum, overNum;
long empName;
double rateNum;
try {
empName = Long.parseLong(employeename.getText());
empNum = Integer.parseInt(employeenumber.getText());
rateNum = Double.parseDouble(hourlyrate.getText());
regularNum = Integer.parseInt(regularhours.getText());
overNum = Integer.parseInt(overtimehours.getText());
ostream.writeLong(empName);
ostream.writeInt(empNum);
ostream.writeDouble(rateNum);
ostream.writeInt(regularNum);
ostream.writeInt(overNum);
employeename.setText("");
employeenumber.setText("");
hourlyrate.setText("");
regularhours.setText("");
overtimehours.setText("");
} catch (NumberFormatException e2) {
System.err.println("Invalid number ");
} catch (IOException e3) {
System.err.println("Error writing file");
System.exit(1);
}
}
public void actionPerformed(ActionEvent e) {
writeRecord();
}
public void windowClosing(WindowEvent e) {
try {
ostream.close();
} catch (IOException e4) {
System.err.println("File not closed");
System.exit(1);
}
System.exit(0);
}
public void windowClosed(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public static void main(String[] args) {
CreateMailOrderFile cmof = new CreateMailOrderFile();
cmof.setVisible(true);
}
}
Here is the note that my instructor sent back:
ostream.writeLong(empName);
empName is not a Long Integer
Please help. Thanks.