我使用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
我应该为此添加(除此之外)。
当我添加员工时,它不会显示上面的员工列表,它根本不会显示该列表。
以下是我目前获得的输出片段,显示它不包含文本文件数据。
S, Ghandhi, Mohandas 1111 150000.0
H, Luther, Maritn 2222 10.5 false false
H, Mandela, Nelson 3333 12.5 true true 40
另外,我不确定为什么在S / H之后会出现逗号。这是什么导致它不添加到文本文件带来的数据?
这是我的主要课程WorkerApp
:
public class WorkerApp{
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[] = new String[7];
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 maxhu = Integer.parseInt(Employee[5]);
e = new HourlyWorker(sorh, name, id, salary, maxhu);
}
else{
e = new HourlyWorker(sorh, name, id, salary);
}
}
company.add(e);
}
}catch (Exception err){
System.out.println("Cannot open EmployeeData.TXT.");
}
System.out.println("REPORT");
company.print();
System.out.println();
System.out.println("1. Adding a salaried worker");
SalariedWorker s1 = new SalariedWorker("S", "Ghandhi, Mohandas", "1111", 150000);
company.add(s1);
System.out.println();
System.out.println("this will trigger the expand capacity method");
HourlyWorker h1 = new HourlyWorker("H", "Luther, Maritn", "2222", 10.5);
company.add(h1);
System.out.println();
System.out.println("3, Adding an hourly worker who has overtime allowed.");
HourlyWorker h2 = new HourlyWorker("H", "Mandela, Nelson", "3333", 12.5, 40);
company.add(h2);
System.out.println();
System.out.println("4. Adding a worker that is already in the database");
try{
company.add(s1);
}catch(Exception e){
System.out.println(e);
System.out.println();
}
System.out.println();
System.out.println("5. Printing the sorted list.");
company.print();
System.out.println();
System.out.println("6,. Removing a worker who is Not in the list.");
SalariedWorker s2 = new SalariedWorker("S", "Parks, Rosa", "5555", 11600);
company.remove("abc");
System.out.println();
System.out.println("7. Removing a worker who is the first in the list");
company.remove("Baron, James");
System.out.println();
System.out.println("8. Finding a worker who is the middle of the list.");
int index = company.find("Newton, Isaac");
System.out.println("Found at "+index);
System.out.println();
System.out.println("9. Finding a worker who is not in the list.");
index = company.find("xyz, abc");
System.out.println("Found at "+index);
System.out.println();
System.out.println("10. Finding the weekly salary of a worker who is salaried");
System.out.println(s1.FindSalary());
System.out.println();
System.out.println("11. FInd the weekly Salary of an hourly worker who has no overtime allowed.");
System.out.println("[Entering a time of 50 hours]");
System.out.println(h1.FindSalary(50));
System.out.println();
System.out.println("12, FInd the weekly Salary of an hourly worker who has overtime allowed.");
System.out.println("[Entering the time of 50 hours]");
System.out.println(h2.FindSalary(50));
System.out.println();
System.out.println("13. FInd the weekly Salary of an hourly worker who has overtime allowed.");
System.out.println("[Entering a time of 20 hours]");
System.out.println(h2.FindSalary(20));
System.out.println();
System.out.println("14. Printing the sorted list.");
company.print();
}
}
也许我的问题出在我的公司课上?
import java.io.*;
import java.util.*;
public class Company{
private Employee[] employeeArray;
private final int InitialCapacity = 7;
private int employCount;
public Company(){
employeeArray = new Employee[InitialCapacity];
employCount = 0;
}
public int size() {
return employCount;
}
public int find(String name){
for (int i = 0; i < employCount; i++){
if (employeeArray[i].getName().equals(name)){
return i;
}
}
return -1;
}
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 ("Employee Not New");
}
}
if (employeeArray.length == employCount){
expand();
}
for(int i = employCount; i > index; i--){
employeeArray[i] = employeeArray[i - 1];
}
employeeArray[index] = employ;
employCount++;
return index;
}
public void remove(String name){
int index = find(name);
if (index == -1){
System.out.println("Employee not Found");
return;
}
for (int i = index; i < employCount - 1; i++){
employeeArray[i] = employeeArray[i + 1];
}
employCount--;
}
public void print(){
if(employCount == 0){
System.out.println("List is Empty");
return;
}
for(int i = 0; i < employCount; i++){
System.out.println(employeeArray[i]);
}
}
public Object get(int index){
if(index >= 0 && index < employCount){
return employeeArray[index];
}else{
return "List out of Bounds";
}
}
private void expand(){
Employee[] newArray = new Employee[employeeArray.length + InitialCapacity];
for (int i = 0; i < employeeArray.length; i++){
newArray[i] = employeeArray[i];
}
employeeArray = newArray;
}
}
提前谢谢!
答案 0 :(得分:2)
您的插入逻辑似乎很奇怪。如果您希望在数组末尾添加新员工(意味着在数组中第一个未占用的位置),则不需要第二个循环。
您只需要检查Employee是否已经在数组中(您的第一个循环),然后将员工添加到index
位置(与employCount
相同)。
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 ("Employee Not New");
}
}
if (employeeArray.length == employCount){
expand();
}
employeeArray[index] = employ;
employCount++;
return index;
}
编辑:
你的问题比我上面提到的要大得多:
String line = reader.nextLine();
String Employee[] = new String[7];
你有一行文件,但没有做任何事情。然后创建一个String数组并开始访问它的值,即使它们都是null。因此,Employee[0].equals("S")
等方法调用会导致NullPointerException
。
你应该做的是:
String line = reader.nextLine();
String Employee[] = line.split(" "); // assuming " " is the separator
// between tokens in each line
现在您的Employee
数组(如果您不想将其与Employee
类混淆,则应该召集员工)将包含该行的列,您可以在循环的其余部分。