2016-11-06 41 views
0

我正在嘗試編寫一個解析文本文件的類,根據員工加入的年份將信息存儲在數組中,並根據每行創建一個員工,推銷員或執行對象。我的教授給我們這條線來分析今年關於解析方法的困惑

while ((line = br.readLine()) != null) 
    { 
     int year = Integer.parseInt(line.substring(0,4)); 
     Employee e = getEmployee(line); 
    } 

而且這種方法解析文檔的其餘部分

public static Employee getEmployee(String line) 
{ 
    Employee e= new Employee() 
    String[] splitWithComma = line.split(","); 
    String first = splitWithComma[0]; 
    String[] firstSplit = first.split(" "); 
    String second = splitWithComma[1]; 
    String[] secondSplit = second.split(" "); 
    String third = splitWithComma[2]; 
    String[] thirdSplit = third.split(" "); 
    String fourth = splitWithComma[3]; 
    String[] fourthSplit = fourth.split(" "); 
    String fifth=splitWithComma[4]; 
    String[] fifthSplit = fifth.split(" "); 


} 

我很困惑,我應該如何初始化Employee對象的getEmployee的類,如果我需要解析任我加倍的方法的同時,如何做到這一點

這裏是我的文本文件

2014, Employee, John Baker, 15000 
2014, Salesman, Amanda Stein, 30000, 1100000 
2014, Executive, Jessica Kettner, 53 
2015, Employee,Zach Edwards, 20000 
2015, Salesman,Shelby Douglas, 45000, 2345 
2015, Executive, Corey Matthews, 67000, 48 

我的員工類

import java.util.*; 

public class Employee 
{ 
private String name; 
private double monthlySalary; 

public Employee(String name, double monthlySalary) 
{ 
    this.name=name; 
    this.monthlySalary=monthlySalary; 
} 



public String getName() { 
    return name; 
} 



public void setName(String name) { 
    this.name = name; 
} 



public double getMonthlySalary() { 
    return monthlySalary; 
} 



public void setMonthlySalary(int MonthlySalary) 
{ 

} 



public double annualSalary() 
{ 
    return monthlySalary*12; 
} 







public String toString() 
{ 
    String str; 
    str="Name: "+name; 
    str+="\nMonthly Salary: "+monthlySalary; 
    return str; 
} 
} 

而且我的司機

import java.io.*; 
import java.util.*; 
public class employeeDriver 
{ 
public static void main(String[] args) 
{ 
    String line; 
    String input; 
    Scanner readInput=null; 
    Scanner readFile = null; 
    BufferedReader br=null; 

    try 
    { 
     br = new BufferedReader(new FileReader("tester.txt")); 
    } 
    catch(FileNotFoundException e) 
    { 
     System.out.println("The file can't be opened"); 
     System.exit(0); 
    } 


try 
{ 
    while ((line = br.readLine()) != null) 
    { 
     int year = Integer.parseInt(line.substring(0,4)); 
     Employee e = getEmployee(line); 
    } 

} 

catch (IOException ioe) 
{ 
    System.out.println("Can't read file"); 
} 
finally 
{ 
    System.exit(0); 
} 

} 


public static Employee getEmployee(String line) 
{ 
    Employee e= new Employee() 
    String[] splitWithComma = line.split(","); 
    String first = splitWithComma[0]; 
    String[] firstSplit = first.split(" "); 
    String second = splitWithComma[1]; 
    String[] secondSplit = second.split(" "); 
    String third = splitWithComma[2]; 
    String[] thirdSplit = third.split(" "); 
    String fourth = splitWithComma[3]; 
    String[] fourthSplit = fourth.split(" "); 
    String fifth=splitWithComma[4]; 
    String[] fifthSplit = fifth.split(" "); 


} 
} 
+0

閱讀['string.split()']的幫助(http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String )),它可以幫助... – Lucero

+0

我知道如何分割的作品,我只是不確定的getEmployee的方法將如何解析文本文件的其餘部分,以及如何將其存儲在我的Employee對象 – WILLO567

+0

'getEmployee'只解析一次一行;文本文件的每一行都被讀取,然後用它進行分析。另外,我對幫助的評論是因爲記錄是用逗號分隔的,而不是空格分隔的,所以看着你發佈的代碼,我有一個印象,它不清楚它是如何工作的。 – Lucero

回答

0

要回答你的第一個問題,Employee構造正在一個String和雙輸入,所以當你創建新的Employee對象你必須(除非你再拍構造函數)提供的值,如:

Employee e = new Employee("John Baker", 15000); 

對於解析位,它看起來像getEmployee的實現方法具d,預計搞定,讓你把你的while循環是這樣的

Employee e = getEmployee(line); 

的getEmployee的方法是不完整的,但。由於您在創建Employee對象時已經需要名稱和工資,因此應該首先進行解析,然後創建對象。

關於解析雙打,有Double.parseDouble(String)方法,但似乎在文本文件中的工資是無論如何格式化爲整數,如果你試圖做類似

int salary = Integer.parseInt(salaryStr); 
Employee e = new Employee("John Baker", salary); 

那麼Java應該自動將整數轉換爲double。薪水應該是班級本身的雙倍還是整數是另一回事。

+0

好吧,所以getEmployee中的信息處理信息。我是否缺少解析信息,如果沒有,我將如何爲員工申報?我會通過三,四次,因爲他們有員工姓名和薪水?我會爲我的推銷員和行政人員提供什麼? – WILLO567

+1

這一切都取決於您對Employee類的要求是什麼。好像你做了很多不必要的分裂的,你需要的是真正的第一「splitWithComma」字符串。爲了反映不同類型的員工,使用Employee超類可能是合適的,然後讓RegularEmployee,Salesman和Executive從中繼承。 – Lidae