2017-12-09 1 views
-1

저는 무엇을하고 있는지 잘 모릅니다. 내가 다음 코드를 사용하여 텍스트 파일을 읽고 있어요 : 세트ArrayList가 예상 데이터를 반환하지 않습니다.

7 
10 416-555-6667 Burgess Smith 15 
15 905-777-8888 Thomas Patel 10 
20 905-111-2222 Morris J. Stevenson 5 
25 416-222-3333 Spencer Larson 30 
30 416-333-4444 Adams Doe 18 
35 905-122-5454 Price Hanks 15 
40 905-343-5151 Clement L. Webster 8 
private static void fileReader() throws FileNotFoundException 
{ 
    int eId = 0; 
    String nme = ""; 
    String phne = ""; 
    int yrs = 0; 
    String line =""; 
    Employee emp = new Employee(eId, nme, phne, yrs); 
    File inputfile = new File("Emp.txt"); 
    Scanner in = new Scanner(inputfile); 
    n = in.nextInt() - 1; 
    in.nextLine(); 
    in.useDelimiter(""); 
    for (int i=0;i<=n;i++)   
    { 
     int l = 0; 
     int m = 0; 
     int n = 0; 
     line = in.nextLine(); 
     while (Character.isDigit(line.charAt(l))) 
     { 
      l++; 
     } 
     m = l + 1; 
     while (!Character.isLetter(line.charAt(m)) && !Character.isWhitespace(line.charAt(m))) 
     { 
      m++; 
     } 
      n = m + 1; 
      while (!Character.isDigit(line.charAt(n))) 
      { 
       n++; 
      } 
      eId = Integer.parseInt(line.substring(0, l)); 
      emp.setEmpId(eId); 
      phne = line.substring(l + 1, m - 1); 
      emp.setTelephone(phne); 
      nme = line.substring(m + 1, n - 1); 
      emp.setName(nme); 
      yrs = Integer.parseInt(line.substring(n)); 
      emp.setYears(yrs); 
      empArr.add(i, emp); 
     } 
    in.close(); 
} 

클래스를 얻을 방법 :

public class Employee 
{ 
     private int empId; 
     private String telephone; 
     private String name; 
     private int yearsOfWork; 
     public Employee(int id, String name, String telephone, int yearsOfWork) 
    { 
    empId = id; 
    this.telephone = telephone; 
    this.name = name; 
    this.yearsOfWork = yearsOfWork; 
    } 
    public void setEmpId(int id) 
    { 
    empId = id; 
    } 
    public void setName(String name) 
    { 
    this.name = name; 
    } 

    public void setTelephone(String telephone) 
    { 
    this.telephone = telephone; 
    } 

    public void setYears(int years) 
    { 
    yearsOfWork = years; 
    } 

    public int getEmpId() 
    { 
    return empId; 
    } 

    public String getName() 
    { 
    return name; 
    } 

    public String getTelephone() 
    { 
    return telephone; 
    } 

    public int getYears() 
    { 
    return yearsOfWork; 
    } 
    public String toString() 
    { 
    return "ID:" + empId + ", name: " + name + ", phone: " + telephone + ", years of work: " + yearsOfWork + "\n"; 
    } 

} 나는 밖에서 내 ArrayList에의 get 메소드를 호출

for 루프에서는 각 색인의 텍스트가 마지막 색인의 텍스트로 겹쳐 쓰여집니다.

저는 생성자와 객체에 대한 몇 가지 기본 개념이 빠져 있다고 생각합니다.

도움을 주시면 감사하겠습니다. 감사.

+0

** what ** ArrayList의 메소드는 무엇입니까? –

+0

당신의 직감이 정확합니다, 당신은 emp 객체 생성을 놓치고 있습니다. emp 객체 생성을 루프로 옮겨야합니다. –

+0

다음은 시도한 사용 예입니다. https://pastebin.com/hc6ciN3R –

답변

0

직감이 정확합니다. 사용자는 emp 개체 생성을 놓치고 있습니다. emp 객체 생성을 루프로 옮겨야합니다.

0
  1. Employee 클래스와 getter/setter 메소드가 올바르게 작성되었습니다.
  2. 는 아래에 주어진 유사 당신을 FileReader() 메소드를 다시 작성

    : -

    String line ="";  
    //Declare an Arraylist for an Employee 
    List<Employee> employee = new ArrayList<Employee>();    
    //Read a file 
    File inputfile = new File("Emp.txt file path");  
    Scanner in = new Scanner(inputfile); 
    
    //Reading a number from a first sentence 
    int n = Integer.parseInt(in.nextLine());  
    
    for (int i=0;i<n;i++) { 
        // Reading each sentence 
        line = in.nextLine(); 
        //Parse an Emp id 
        int eId = Integer.parseInt(line.substring(0, 2)); 
        //Parse a phone number 
        String phone = line.substring(3, 14); 
        //Parse a name   
        String name = line.split("\\d+")[4];   
        //Parse years 
        int years = Integer.parseInt(line.split("\\D+")[4]); 
        //Now create an object by putting all above values in a constructor 
        Employee emp1 = new Employee(eId, name, phone, years); 
        //Add that object in an arraylist 
        employee.add(emp1); 
    } 
    
    //As you have overridden toString method, print an arraylist 
    System.out.println(emp.toString()); 
    //Closing the scanner 
    in.close(); 
    } 
    

희망이 도움이됩니다.

관련 문제