我使用BlueJ作为参考。
该程序编译并运行良好。
问题是我应该打开一个文本文件(并添加到它)但我得到一个例外,它说我无法打开所述文件。
该程序应该读取并打开“EmployeeData.txt”:
S Washington,George 000001 125000
H MacDonald,Ronald 386218 7.80 true 40
H Walton,Samuel 268517 8.21 false
H Thomas,David 131313 9.45 true 38
H Sanders,HarlandDavid 277651 8.72 false
S Baron,James 368535 310236
这是我目前获得的输出片段:
Cannot open EmployeeData.txt.
1) Add a salaried worker
S Moran,Blake 123456 260000.0
2) Add an hourly worker who has no overtime allowed
S Moran,Blake 123456 260000.0
H Bob,Billy 654321 15.0 false
3) Add an hourly worker who has overtime allowed
S Moran,Blake 123456 260000.0
H Bob,Billy 654321 15.0 false
H Smith,Will 345612 10.5 true 30
4) Add a worker that is already in the database
java.lang.RuntimeException: The Employee Is Not New
我正在尝试阅读并打开我的主要课程WorkerApp
中的文本文件:
public class WorkerApp{
/**
* Reads the infile, runs tests, and prints the output.
*/
public static void main (String args[]){
Company company = new Company();
try{
Scanner reader = new Scanner (new File("EmployeeData.txt"));
while(reader.hasNext()){
String line = reader.nextLine();
String Employee[] = line.split(" ");
String sorh = Employee[0];
String name = Employee[1];
String id = Employee[2];
double salary = Double.parseDouble(Employee[3]);
Employee e;
if (Employee[0].equals("S")){
e = new SalariedWorker(sorh, name, id, salary);}
else {
boolean overtime = Boolean.parseBoolean(Employee[4]);
if(overtime){
int maxHours = Integer.parseInt(Employee[5]);
e = new HourlyWorker(sorh, name, id, salary, maxHours);
}
else{
e = new HourlyWorker(sorh, name, id, salary);
}
}
company.add(e);
}
}catch (Exception err){
System.out.println("Cannot open EmployeeData.txt.");
}
//Test Number 1
System.out.println("1) Add a salaried worker");
SalariedWorker SWorker1 = new SalariedWorker("S", "Moran,Blake", "123456", 260000);
company.add(SWorker1);
company.print();
//Test Number 2
System.out.println("2) Add an hourly worker who has no overtime allowed");
HourlyWorker HWorker1 = new HourlyWorker("H", "Bob,Billy", "654321", 15);
company.add(HWorker1);
company.print();
//Test Number 3
System.out.println("3) Add an hourly worker who has overtime allowed");
HourlyWorker HWorker2 = new HourlyWorker("H", "Smith,Will", "345612", 10.5, 30);
company.add(HWorker2);
company.print();
//Test Number 4
System.out.println("4) Add a worker that is already in the database");
try{
company.add(SWorker1);
}catch(Exception err){
System.out.println(err);
System.out.println();
}
//Test Number 5
System.out.println("5) Print the sorted list");
company.print();
//Test Number 6
System.out.println("6) Remove a worker who is NOT in the list");
company.remove("Brooks,Phil");
System.out.println();
//Test Number 7
System.out.println("7) Remove a worker who is the first in the list ");
company.remove("Moran,Blake");
company.print();
System.out.println();
//Test Number 8
System.out.println("8) Find a worker who is the middle of the list");
int index = company.find("Bob,Billy");
System.out.println("Found at "+ index);
System.out.println();
//Test Number 9
System.out.println("9) Find a worker who is NOT in the list");
index = company.find("Harrison,Ford");
System.out.println("Found at "+ index);
System.out.println();
//Test Number 10
System.out.println("10) Find the weekly salary of a worker who is salaried");
System.out.println(SWorker1.FindSalary());
System.out.println();
//Test Number 11
System.out.println("11) Find the weekly salary of an hourly worker who has no overtime allowed [50 hours]");
System.out.println(HWorker1.FindSalary(50));
System.out.println();
//Test Number 12
System.out.println("12) Find the weekly salary of an hourly worker who has overtime allowed [50 hours]");
System.out.println(HWorker2.FindSalary(50));
System.out.println();
//Test Number 13
System.out.println("13) Find the weekly salary of an hourly worker who has overtime allowed [20 hours]");
System.out.println(HWorker2.FindSalary(20));
System.out.println();
//Test Number 14
System.out.println("14) Print the sorted list");
company.print();
//Test Number 15
System.out.println("\n15) End the process");
}
}
如果有帮助,这是我的Company
课程:
public class Company{
private Employee[] employeeArray;
private final int InitialCapacity = 7;
private int employCount;
/**
* Creates the employee array and sets employCount to 0.
*/
public Company(){
employeeArray = new Employee[InitialCapacity];
employCount = 0;
}
/**
* Finds an employee in the list.
*/
public int find(String name){
for (int i = 0; i < employCount; i++){
if (employeeArray[i].getName().equals(name)){
return i;
}
}
return -1;
}
/**
* Adds an employee to the list.
*/
public int add(Employee employ){
int index;
for (index = 0; index < employCount; index++){
int result = employeeArray[index].getName().compareTo(employ.getName());
if(result == 0){
throw new RuntimeException ("The Employee Is Not New");
}
}
if (employeeArray.length == employCount){
expand();
}
employeeArray[index] = employ;
employCount++;
return index;
}
/**
* Removes an employee to the list.
*/
public void remove(String name){
int index = find(name);
if (index == -1){
System.out.println("The Employee is not Found");
return;
}
for (int i = index; i < employCount - 1; i++){
employeeArray[i] = employeeArray[i + 1];
}
employCount--;
}
/**
* Prints the list.
*/
public void print(){
if(employCount == 0){
System.out.println("The List is Empty");
return;
}
for(int i = 0; i < employCount; i++){
System.out.println(employeeArray[i]);
}
}
/**
* Expands the list.
*/
private void expand(){
Employee[] newArray = new Employee[employeeArray.length + InitialCapacity];
for (int i = 0; i < employeeArray.length; i++){
newArray[i] = employeeArray[i];
}
employeeArray = newArray;
}
}
应该注意的是,还有其他三个类Employee
,HourlyWorker
和SalariedWorker
。梯形图2是Employee的子类,但它们在读取文本文件方面并不重要,所以我将它们从这篇文章中排除。
希望我能得到一些帮助。
提前谢谢!
答案 0 :(得分:3)
写
真的很危险catch (Exception err){
System.out.println("Cannot open EmployeeData.txt.");
}
或类似的东西,因为它意味着你扔掉了导致错误的所有信息。至少,您应该打印出错误的堆栈跟踪,以便您可以调查发生的情况。
尝试添加
err.printStackTrace();
在catch
块内并读取它打印的内容。这应该告诉你出了什么问题。
答案 1 :(得分:0)
我注意到你的代码中有一些错误,例如System.out.println(employeeArray[i]);
,它会打印出一些内存废话,所以我去了一些代码,并添加了FileNotFoundException
的堆栈跟踪。他是我所做程序的解决方案。
package helper;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class WorkerApp {
/**
* Reads the infile, runs tests, and prints the output.
*/
public static void main(String args[]) {
Company company = new Company();
Employee emp;
try {
BufferedReader read = new BufferedReader(new InputStreamReader(
new FileInputStream("EmployeeData.txt")));
String line;
while ((line = read.readLine()) != null) {
String[] current = line.split(" ");
emp = new Employee(current[0], current[1], current[2],
current[3]);
if (emp.getSorh().equals("S")) {
emp = new SalariedWorker(emp.getSorh(), emp.getName(),
emp.getId(), emp.getSalary());
} else {
if (Boolean.parseBoolean(current[4])) {
emp = new HourlyWorker(emp.getSorh(), emp.getName(),
emp.getId(), emp.getSalary(),
Integer.parseInt(current[5]));
} else {
emp = new HourlyWorker(emp.getSorh(), emp.getName(),
emp.getId(), emp.getSalary());
}
}
company.add(emp);
}
} catch (Exception err) {
System.out.println("Cannot open EmployeeData.txt.");
System.out.println("Got an IOException: " + err.getMessage());
}
}
}
公司类
package helper;
import java.util.ArrayList;
public class Company {
private ArrayList<Employee> comp = new ArrayList<Employee>();
private int employeeCount = comp.size();
public void add(Employee emp) {
boolean found = false;
for (Employee employee : comp) {
if (employee.getName().equals(emp)) {
System.out.println("Employee is not new");
found = true;
break;
}
}
if (!found) {
comp.add(emp);
employeeCount = comp.size();
}
}
public void print() {
if (comp.size() >= 0) {
for (Employee employee : comp) {
System.out.println("Employee Name: " + employee.getName());
System.out.println("Employee Id: " + employee.getId());
}
} else {
System.out.println("You have no Employees");
}
}
public void remove(String emp) {
int found = find(emp);
if(!(found == -1){
comp.remove(found);
employeeCount = comp.size();
} else {
System.out.println("No such employee exists");
}
}
public int find(String emp) {
int i = 0;
for (Employee employee : comp) {
if (employee.getName().equals(emp)) {
break;
}
i++;
}
return -1;
}
}
员工类
package helper;
public class Employee {
private String sorh;
private String name;
private String id;
private double salary;
public Employee(String sorh, String name, String id, String salary) {
this.sorh = sorh;
this.name = name;
this.id = id;
this.salary = Double.parseDouble(salary);
}
public String getSorh() {
return sorh;
}
public void setSorh(String sorh) {
this.sorh = sorh;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
SalariedWorker Class
package helper;
public class SalariedWorker extends Employee {
public SalariedWorker(String sorh, String name, String id, double salary) {
super(sorh, name, id, String.valueOf(salary));
}
}
HourlyWorker CLass
package helper;
public class HourlyWorker extends Employee{
private int maxHours;
public HourlyWorker(String sorh, String name, String id, double salary) {
super(sorh, name, id, String.valueOf(salary));
// TODO Auto-generated constructor stub
}
public HourlyWorker(String sorh, String name, String id, double salary,
int maxHours) {
super(sorh, name, id, String.valueOf(salary));
this.maxHours = maxHours;
}
}