2016-11-06 4 views
0

텍스트 파일을 구문 분석하고 직원이 합류 한 연도를 기반으로 배열에 정보를 저장하고 각 줄을 기반으로 직원, 세일즈맨 또는 경영진 개체를 만드는 클래스를 작성하려고합니다. 내 교수는 올해구문 분석 방법에 대해 혼동 됨

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

구문 분석하는 우리에게이 줄을했다 그리고이 방법은 내가이 GetEmployee라는 클래스에서 직원 개체를 초기화하는 방법에 대한 혼란 스러워요

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


} 

문서의 나머지 부분을 구문 분석 , 내가있는 동안 방법의 모든 내 복식을 분석해야하는 경우, 그리고 수행하는 방법과

여기 내 텍스트 파일 있다는

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

첫 번째 질문에 대답하기 위해, 직원 생성자는 새 직원 Object를 작성 그렇게 할 때, 문자열을 복용하고 입력으로 더블 (다른 생성자를하지 않는 한)이 같은, 그 값을 제공해야합니다 : 구문 분석 비트

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로 변환해야합니다. 급여가 클래스 자체의 이중 또는 int 여야하는지 여부는 또 다른 이야기입니다.

+0

OK, getEmployee의 정보가 정보를 처리합니다. 파싱에 대한 정보가 더 이상 누락 되었습니까? 아니라면 어떻게 직원에게 알릴 수 있습니까? 직원 명과 급여가 있기 때문에 3 ~ 4 개를 통과 할 수 있습니까? 세일즈맨 및 이그제큐티브 하위 클럭의 경우 어떻게해야합니까? – WILLO567

+1

그것은 모두 여러분의 요구 사항이 Employee 클래스의 시작에 달려있다. 불필요한 분할을 많이하는 것처럼 보입니다. 필요한 모든 것이 실제로 첫 번째 "splitWithComma"문자열에있는 것 같습니다.여러 유형의 직원을 반영하려면 Employee 수퍼 클래스를 사용하고 RegularEmployee, Salesman 및 Executive에서 해당 유형을 상속받는 것이 적절할 수 있습니다. – Lidae