【问题标题】:Confused about parsing method对解析方法感到困惑
【发布时间】:2017-03-20 04:42:13
【问题描述】:

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

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


} 
}

【问题讨论】:

  • 阅读string.split() 的帮助,它可能会有所帮助...
  • 我了解 split 的工作原理,我只是不确定 getEmployee 方法将如何解析文本文件的其余部分,以及如何将其存储在我的 Employee 对象中
  • getEmployee 一次只解析一行;读取文本文件的每一行,然后对其进行解析。另外,我对帮助的评论是因为记录是逗号分隔的,而不是空格分隔的,所以看着你发布的代码,我的印象是不清楚它是如何工作的。
  • 好的,我对其进行了编辑,使其以空格分隔。唯一有逗号的是员工的姓名。 getEmployee 中的哪一行是 String[] splitWithComma = line.split(",");申请了吗?

标签: java arrays parsing text split


【解决方案1】:

为了回答您的第一个问题,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 还是 int 是另一回事。

【讨论】:

  • OK,getEmployee 中的信息处理了这些信息。我是否缺少更多用于解析的信息,如果没有,我将如何为员工声明它?我会通过三和四,因为他们有员工姓名和薪水吗?我会为我的销售员和执行人员子类做什么?
  • 这完全取决于您对 Employee 类的要求。看起来你做了很多不必要的分割,你真正需要的只是第一个“splitWithComma”字符串。为了反映不同类型的员工,可能适合使用 Employee 超类,然后让 RegularEmployee、Salesman 和 Executive 继承自它。
猜你喜欢
  • 2017-09-08
  • 1970-01-01
  • 2013-07-27
  • 2019-09-22
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
相关资源
最近更新 更多