텍스트 파일을 구문 분석하고 직원이 합류 한 연도를 기반으로 배열에 정보를 저장하고 각 줄을 기반으로 직원, 세일즈맨 또는 경영진 개체를 만드는 클래스를 작성하려고합니다. 내 교수는 올해구문 분석 방법에 대해 혼동 됨
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(" ");
}
}
[string.split()]에 대한 도움말보기 (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split (java.lang.String)), 도움이 될 수도 있습니다 ... – Lucero
어떻게 분할 작동하는지, getEmployee 메서드는 나머지 텍스트 파일을 구문 및 내 Employee 개체에 저장하는 방법에 대한 확실하지 오전 – WILLO567
'getEmployee' 구문 분석 전용 한 번에 한 줄; 텍스트 파일의 각 행이 읽히고 그 다음에 파싱됩니다. 또한 도움말에 대한 제 의견은 공백으로 구분하지 않고 쉼표로 구분하여 기록한 것이므로 게시 한 코드를 보면 어떻게 작동하는지 명확하지 않다는 인상을 받았습니다. – Lucero