2016-11-06 174 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