Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trouble with Java payroll program

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Trouble with Java payroll program

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with Java payroll program

    Also you need to post the full text of the error message that you are getting.

    Did you read the API doc for the writeLong() method that the note from your instructor mentions and compare what the API doc says with what you coded?

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Trouble with Java payroll program

    Quote Originally Posted by Phynx View Post
    you ever solve this?
    if not, try these changes...
    empName = employeename.getText();
    ostream.writeUTF(empName);



    empName = employeename.getText();
     empNum = Integer.parseInt(employeenumber.getText());
     rateNum = Double.parseDouble(hourlyrate.getText());
     regularNum = Integer.parseInt(regularhours.getText());
     overNum = Integer.parseInt(overtimehours.getText());
     ostream.writeUTF(empName);
     ostream.writeInt(empNum);
     ostream.writeDouble(rateNum);
     ostream.writeInt(regularNum);
     ostream.writeInt(overNum);
     
     employeename.setText("");
     employeenumber.setText("");
     hourlyrate.setText("");
     regularhours.setText("");
     overtimehours.setText("");
    This only happens when you don't read the Forums Rules. DON'T SPOONFEED

Similar Threads

  1. [SOLVED] Please help me with this payroll program
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 06:47 PM
  2. heeeeeyyyy .. HELP ! :) -- PAYROLL SYSTEM --
    By princess in forum Java Theory & Questions
    Replies: 5
    Last Post: February 27th, 2011, 03:11 PM
  3. Trouble with law of Cosine program
    By Delstateprogramer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 06:07 PM
  4. Payroll
    By Kesh486 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 6th, 2010, 06:48 PM
  5. Trouble in downloading java
    By captjade in forum Java Theory & Questions
    Replies: 6
    Last Post: March 3rd, 2009, 04:16 PM