2009-11-14 109 views
0

我创建了输出的程序,允许用户输入一个Java I/O程序try-catch块有问题的员工的姓名和电话号码,然后他们的小时工资和他们的正常工作时间和加班时间总数小时。这是我第一次在Java中这种类型的程序的工作,我在用try-catch块那里得到来自用户的文本输入的问题,然后将其设置。如果任何人都可以帮助我,将不胜感激。

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class CreatePayrollFile 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 Payroll Information"); 
    private JTextField employeeName = new JTextField(10); 
    private JTextField employeeNumber = new JTextField(10); 
    private JTextField hourlyRate = new JTextField(10); 
    private JTextField regularHours = new JTextField(10); 
    private JTextField overtimeHours = new JTextField(10); 
    private JLabel enameLabel = new JLabel("Employee Name  "); 
    private JLabel enumLabel = new JLabel("Employee Number "); 
    private JLabel hrLabel = new JLabel("Hourly Rate    "); 
    private JLabel rhLabel = new JLabel("Regular Hours   "); 
    private JLabel orLabel = new JLabel("Overtime Hours  "); 
    private JButton enterDataButton = new JButton(" Enter data "); 
    DataOutputStream ostream; 

    public CreatePayrollFile() { 
     super("Create Payroll 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(enameLabel); 
     dPanel2.add(employeeName); 
     displayPanel.add(dPanel2); 

     dPanel3.add(enumLabel); 
     dPanel3.add(employeeNumber); 
     displayPanel.add(dPanel3); 

     dPanel4.add(hrLabel); 
     dPanel4.add(hourlyRate); 
     displayPanel.add(dPanel4); 

     dPanel5.add(rhLabel); 
     dPanel5.add(regularHours); 
     displayPanel.add(dPanel5); 

     dPanel6.add(orLabel); 
     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() { 

     String employeeName; 
     Double hourlyRate; 
     Integer employeeNumber, regularHours, overtimeHours; 


    // this is where I am having errors. It is saying that the getText method is undefined 
    // for each respective use (int, string, double). I am also getting error "The method 
    // writeString(String) is undefined for the type DataOutputStream" and the same 
    // undefined errors for the setText for each respective use. 

     try { 
      employeeName = String.parseString(employeeName.getText()); 
      employeeNumber = Integer.parseInt(employeeNumber.getText()); 
      hourlyRate = Double.parseDouble(hourlyRate.getText()); 
      regularHours = Integer.parseInt(regularHours.getText()); 
      overtimeHours = Integer.parseInt(overtimeHours.getText()); 
      ostream.writeString(employeeName); 
      ostream.writeInt(employeeNumber); 
      ostream.writeDouble(hourlyRate); 
      ostream.writeInt(regularHours); 
      ostream.writeInt(overtimeHours); 
      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) { 
     CreatePayrollFile cmof = new CreatePayrollFile(); 
     cmof.setVisible(true); 
    } 
} 

因为我已经在之前在这个论坛上张贴功课援助请求,“啪”,我指出事先我知道这是功课,但任何人都可以看到,我不只是问让人们去做。我觉得不得不说这是夸夸其谈,但我不想让任何人认为我没有付出努力。我已经把精力完成一切形式为教练教,但我必须有误解,当他走到覆盖集讲座的一部分,get方法。

在此先感谢所有谁提供援助。


+++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++


得益于援助来自各个论坛的程序现在可以工作并创建它应该输出的文件。现在我需要创建一个读入刚刚创建的文件的程序。我没有把它全部放在第一篇文章中,以便能够将事情分解成若干步骤(如果您还没有创建文件,则无法读取文件。如果我需要创建一个新的线程,那么我会,但是由于对程序的熟悉可能会读取这个,所以我将它作为编辑贴在同一个线程中。

我现在需要做的是对其他字段添加到列表的底部显示的工资总额基础上,从刚创建的文件的读取输入已计算后的GUI。

重词,第一个方案是创建文件。现在我需要阅读它并显示计算出的总工资。 (链接到例如输出的图像的photobucket:[URL = “http://i108.photobucket.com/albums/n24/zypher89/readPayroll.jpg”] http://i108.photobucket.com/albums/n24/zypher89/readPayroll.jpg[/URL]

出于某种原因,java.sun.com站点(至少我不能从不同网络上的不同计算机访问它),所以我没有将它作为参考。

这是我的有错误的代码注释

[CODE]

包assignment10_11;

import java.io. ; import java.awt。; import java.awt.event。 ; import javax.swing。;

公共类ReadPayrollFile扩展JFrame中实现的ActionListener,WindowListener的{

public static final int WIDTH = 400; 
public static final int HEIGHT = 300; 
JPanel titlePanel = new JPanel(); 
JPanel displayPanel = new JPanel(new GridLayout(7, 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 Payroll Information"); 
private JTextField employeeName = new JTextField(10); 
private JTextField employeeNumber = new JTextField(10); 
private JTextField hourlyRate = new JTextField(10); 
private JTextField regularHours = new JTextField(10); 
private JTextField overtimeHours = new JTextField(10); 
private JTextField grossPay = new JTextField(10); 
private JLabel enameLabel = new JLabel("Employee Name  "); 
private JLabel enumLabel = new JLabel("Employee Number "); 
private JLabel hrLabel = new JLabel("Hourly Rate    "); 
private JLabel rhLabel = new JLabel("Regular Hours   "); 
private JLabel orLabel = new JLabel("Overtime Hours  "); 
private JLabel gpLabel = new JLabel ("Gross Pay     "); 
private JButton nextRecordButton = new JButton(" Next Record "); 
DataInputStream fstream; 

public ReadPayrollFile() { 
    super("Read Payroll File - Assignment 11"); 

    setSize(WIDTH, HEIGHT); 
    try 
    { 
     fstream = new DataInputStream(new FileInputStream("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(enameLabel); 
    dPanel2.add(employeeName); 
    displayPanel.add(dPanel2); 

    dPanel3.add(enumLabel); 
    dPanel3.add(employeeNumber); 
    displayPanel.add(dPanel3); 

    dPanel4.add(hrLabel); 
    dPanel4.add(hourlyRate); 
    displayPanel.add(dPanel4); 

    dPanel5.add(rhLabel); 
    dPanel5.add(regularHours); 
    displayPanel.add(dPanel5); 

    dPanel6.add(orLabel); 
    dPanel6.add(overtimeHours); 
    displayPanel.add(dPanel6); 

    dPanel7.add(gpLabel); 
    dPanel7.add(grossPay); 
    displayPanel.add(dPanel7); 

    buttonPanel.setBackground(Color.white); 
    buttonPanel.setLayout(new FlowLayout()); 
    nextRecordButton.setMnemonic(KeyEvent.VK_E); 
    buttonPanel.add(nextRecordButton); 
    nextRecordButton.addActionListener(this); 

    contentPane.add(titlePanel, BorderLayout.NORTH); 
    contentPane.add(displayPanel, BorderLayout.CENTER); 
    contentPane.add(buttonPanel, BorderLayout.SOUTH); 
    addWindowListener(this); 
} 

private void readRecord(DataInputStream inputFile) { 

    String l_employeeName; 
    Double l_hourlyRate; 
    Integer l_employeeNumber, l_regularHours, l_overtimeHours; 
    boolean endOfFile = false; 

    try { 

     while (!endOfFile) 
     { 
      try 
      { 
       /** This is where it says that the readChars() is undefined. The code posted 
       * doesn't show it, but I have tried changing the output file program to 
       * write the string as a UTF and then changed this program to readUTF and it 
       * works for "l_employeeName = inputFile.readChars();" but not for the 
       * "fstream.readChars(l_employeeName);". Also, all of the fstream lines have 
       * errors "The method readInt() in the type DataInputStream is not applicable 
       * for the arguments (Integer)" (replace Integer/Int with Double respectively). 
       */ 

       l_employeeName = inputFile.readUTF(); 
       l_employeeNumber = inputFile.readInt(); 
       l_hourlyRate = inputFile.readDouble(); 
       l_regularHours = inputFile.readInt(); 
       l_overtimeHours = inputFile.readInt(); 
       fstream.readUTF(l_employeeName); 
       fstream.readInt(l_employeeNumber); 
       fstream.readDouble(l_hourlyRate); 
       fstream.readInt(l_regularHours); 
       fstream.readInt(l_overtimeHours); 

       calculateGrossPay(l_hourlyRate, l_regularHours, l_overtimeHours); 

       employeeName.setText("l_employeeName"); 
       employeeNumber.setText("l_employeeNumber"); 
       hourlyRate.setText("l_hourlyRate"); 
       regularHours.setText("l_regularHours"); 
       overtimeHours.setText("l_overtimeHours"); 
       grossPay.setText("grossPayAmmount"); 
      } 

      catch (NumberFormatException e2) 
      { 
       System.err.println("Invalid number "); 
      } 

      catch (IOException e3) 
      { 
       System.err.println("Error reading file"); 
       System.exit(1); 
      } //here I get error "Syntax error, insert "Finally" to complete TryStatement" 
     } 

     /** Then here I assume the instructor wants the program to 
     * read in the next set of employee data from the input file 
     * but he didn't specify and hasn't returned my email. So if 
     * going with my assumption, how would I go about doing that? 
     */ 
     public void actionPerformed(ActionEvent e) { 
      NextRecord(); 
     } 

     public void windowClosing(WindowEvent e) { 
      try { 
       fstream.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) { 
      ReadPayrollFile cmof = new ReadPayrollFile(); 
      cmof.setVisible(true); 
     } 
     public double calculateGrossPay(double l_hourlyRate, int l_regularHours, int l_overtimeHours) 
     { 
      double grossPayAmmount, overtimePayRate, overtimePay; 

      overtimePayRate = l_hourlyRate * 1.5; 
      overtimePay = l_overtimeHours * overtimePayRate; 
      grossPayAmmount = ((l_hourlyRate * l_regularHours) + overtimePay); 

      return grossPayAmmount; 
     } 
    } 

[/ CODE]

,我要继续工作,并研究文件I/O,以及试图让API工作。

再次感谢大家的帮助。

+0

你是什么意思与“有问题”吗?编译错误?运行时异常?不受欢迎的行为?如果是后者,那么观察到的行为是什么? – meriton 2009-11-14 13:03:26

回答

2

对于所有setText方法:您有局部变量覆盖类变量。试试这个:

private void writeRecord() { 

    String l_employeeName; 
    Double l_hourlyRate; 
    Integer l_employeeNumber, l_regularHours, l_overtimeHours; 


// this is where I am having errors. It is saying that the getText method is undefined 
// for each respective use (int, string, double). I am also getting error "The method 
// writeString(String) is undefined for the type DataOutputStream" and the same 
// undefined errors for the setText for each respective use. 

    try { 
     l_employeeName = String.parseString(employeeName.getText()); 
     l_employeeNumber = Integer.parseInt(employeeNumber.getText()); 
     l_hourlyRate = Double.parseDouble(hourlyRate.getText()); 
     l_regularHours = Integer.parseInt(regularHours.getText()); 
     l_overtimeHours = Integer.parseInt(overtimeHours.getText()); 
     ostream.writeString(l_employeeName); 
     ostream.writeInt(l_employeeNumber); 
     ostream.writeDouble(l_hourlyRate); 
     ostream.writeInt(l_regularHours); 
     ostream.writeInt(l_overtimeHours); 
     employeeName.setText(""); 
     employeeNumber.setText(""); 
     hourlyRate.setText(""); 
     regularHours.setText(""); 
     overtimeHours.setText(""); 

对于ostream.writeString:本documentation for DataOutputStream表明,没有ostream.writeString。尝试使用ostream.writeChars

+0

该代码的工作原理,但它仍然给我的错误“方法parseString(字符串)未定义类型字符串”为employeeName字符串。我会查看发布的链接中的信息。我正在使用Eclipse Galileo b/c,这是教师告诉我们习惯使用的(而他教导使用NetBeans),如果这就是您所指的。 – Ryujin89 2009-11-14 08:34:58

+0

请看看Integer.parseInt和Double.parseDouble的API文档。我怀疑你可能不明白他们在做什么。然后问问你自己想要用不存在的String.parseString做什么。 – nojo 2009-11-14 14:24:36

0

看一看java的api

有了一些研究,你会发现为什么

... gettext的方法是不明确的

你的字符串

但我建议你使用IDE,甚至作业。

1

问题是,你想访问成员变量,但实际上访问本地变量。局部变量的名字实际上隐藏了你的成员变量!如果你定义这样一个类

public class A { 
    private int asdf = 0; 

    public void blah() { 
    String asdf = "Hello!"; 
    System.out.println(text); 
    } 

} 

该程序将打印“你好!”如果你叫“blah”。这是因为局部变量“text”隐藏了同名的成员。你有两个选择:

  1. 选择不同的名称,这样

    public class A { 
        private int aNumber = 0; 
    
        public void blah() { 
        String asdf = "Hello!"; 
        System.out.println(aNumber); 
        } 
    
    } 
    
  2. 使用此访问该成员像 这

    public class A { 
         private int asdf = 0; 
    
         public void blah() { 
         String asdf = "Hello!"; 
         System.out.println(this.asdf); 
         } 
    
        } 
    
+0

+1表示“此”。 - 这使得代码变得脆弱起来。 – 2009-11-14 11:00:47

2

你犯了3个基本错误

  1. writeRecord()中的本地变量与实例变量具有相同的名称,因此隐藏了它们。已由Chip Uni +解决方案指出。
  2. String类没有功能parseString(String)。检查javadoc(JDK API: String)。只要将其完全删除即可,getText()无论如何返回String
  3. DataOutputStream没有功能writeString()使用writeChars()代替