对解析方法感到困惑

时间:2016-11-06 22:00:06

标签: java arrays parsing text split

我正在尝试编写一个解析文本文件的类,根据员工加入的年份将信息存储在数组中,并根据每一行创建一个employee,salesman或executive对象。我的教授给了我们这一行来解析这一年

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(" ");


} 

我对如何初始化getEmployee类中的Employee对象感到困惑,如果我需要在while方法中解析任何我的双打,以及如何做到这一点

这是我的文字文件

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(" ");


} 
}

1 个答案:

答案 0 :(得分:0)

要回答第一个问题,Employee构造函数将String和double作为输入,因此当您创建新的Employee对象时,您必须(除非您创建另一个构造函数)提供这些值,如下所示:

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

对于解析位,看起来似乎是getEmployee方法处理那个,所以你在while循环中放入的所有内容都是这个

Employee e = getEmployee(line);

但getEmployee方法不完整。由于在创建Employee对象时已经需要名称和工资,因此应首先进行解析,然后创建对象。

关于解析双精度数,有一个Double.parseDouble(String)方法,但似乎在你的文本文件中,薪水无论如何都被格式化为整数,如果你尝试做类似的事情

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

然后Java应该自动将整数转换为double。另一个故事是,薪水应该是班级本身的双倍还是整数。