- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经为一个程序创建了输出,该程序允许用户输入他们的员工姓名和编号,然后输入他们的小时工资以及他们的正常工作时间和加类时间的总数。这是我第一次在 java 中使用此类程序,我遇到了 Try-Catch block 的问题,它从用户那里获取文本输入,然后进行设置。如果有人可以帮助我,我将不胜感激。
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);
}
}
因为我之前在这个论坛上因发布家庭作业协助请求而被“抓拍”,所以我提前声明我知道这是家庭作业,但正如任何人都可以看到的,我不仅仅是要求人们做它。我觉得这样说只是说说而已,但我不想让任何人认为我没有付出努力。我已经按照老师所教的形式付出了努力并完成了所有内容,但是当他回顾了讲座中涉及 set 和 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 站点已经关闭(至少我什至无法从不同网络上的不同计算机访问它),所以我没有它作为引用。
这是我的代码,已注释掉错误
[代码]
包分配10_11;
导入java.io。;导入java.awt。;导入 java.awt.event.;导入 javax.swing.;
公共(public)类 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;
}
}
[/代码]
我将继续研究它并研究文件 I/O 并尝试让 API 正常工作。
再次感谢大家的帮助。
最佳答案
对于所有 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
。
关于java - Java I/O 程序中的 Try-Catch block 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1733651/
我刚刚遇到了一个非常奇怪的行为。这是代码: // So far everything's fine val x: Try[Try[Unit]] = Try(Try{}) x: scala.util.T
“输出”是一个序列化的 OpenStruct。 定义标题 try(:output).try(:data).try(:title) 结束 什么会更好? :) 最佳答案 或者只是这样: def title
我有以下元组 - (t1,t2) :(Try,Try) 我想检查两者是否成功或其中之一是否失败,但避免代码重复。像这样的东西: (t1,t2) match { case (Success(v1),Su
是否必须放置内部 try-with-resources 或其中一个 try-with-resources 中的所有内容都会自动关闭? try (BasicDataSource ds = Bas
有一点特殊,尝试创建一段 try catch 代码来处理 GoogleTokenResponse,但编译器在 try 时抛出异常错误。有什么想法吗? 错误信息: | Loading Grails 2.
它几乎可以在所有语言中找到,而且我大部分时间都在使用它。 我不知道它是内部的,不知道它是如何真正起作用的。 它如何在任何语言的运行时在 native 级别工作? 例如:如果在 try 内部发生 sta
为什么在 readFile2() 中我需要捕获 FileNotFoundException 以及稍后由 close( ) 方法,并且在 try-with-resources(inside readfi
我正在使用 Apache POI 尝试读取 Word 文件,但即使您使用过 Apache POI,这仍然应该是可以回答的。在 HWPF.extractor 包中有两个对象:WordExtractor
如果try-catch的catch block 中抛出异常,那么finally block 会被调用吗? try { //some thing which throws error } cat
这个问题已经有答案了: What's the purpose of try-with-resources statements? (7 个回答) 已关闭 3 年前。 我一直在查看代码,并且已经看到了对
这个问题已经有答案了: What's the purpose of try-with-resources statements? (7 个回答) 已关闭 3 年前。 我一直在查看代码,并且已经看到了对
我正在使用 Try::Tiny尝试捕捉。 代码如下: use Try::Tiny; try { print "In try"; wrongsubroutine(); # undefi
我想知道这样的代码是否会在抛出异常后总是中断而不继续运行,因此代码不会继续执行第二个 temp.dodaj(b)。 Avto *a = new Avto("lambo",4); Avt
我知道在try子句中必须有一个与资源关联的变量声明。 但是除了被分配一个通常的资源实例化之外,它是否可以被分配一个已经存在的资源,例如: public String getAsString(HttpS
我有一个写的方法。此方法仅扫描用户输入的整数输入。如果用户输入一个字符值,它将抛出一个输入不匹配异常,这是在我的 Try-Catch 语句中处理的。问题是,如果用户输入任何不是数字的东西,然后抛出异常
我注意到这不会编译: PrintWriter printWriter = new PrintWriter("test.txt"); printWriter.append('a'); printWrit
我经常看到人们写这样的代码: try: some_function() except: print 'something' 当我认为这样做更干净时: try: some_functio
该应用程序将在第二个显示器上正常显示内容。问题是当我旋转 iPad 时内容不会在 iPad 上旋转。 看过: http://developer.apple.com/library/ios/#qa/qa
我正在学习 java,我发现我不喜欢的一件事通常是当我有这样的代码时: import java.util.*; import java.io.*; public class GraphProblem
我使用 C++ 有一段时间了,对普通的 try/catch 很熟悉。但是,我现在发现自己在 Windows 上,在 VisualStudio 中编码以进行 COM 开发。代码的几个部分使用了如下内容:
我是一名优秀的程序员,十分优秀!