我使用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;
}
}
預先感謝您!
執行此代碼後,遺憾的是沒有任何更改。仍然不輸出文本文件以及添加的員工,它仍然/僅顯示添加的員工。 – BBladem83 2014-11-07 14:50:28