2017-12-09 8 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

は私試みた使用例でありますphone - > phne、name - > nmeのような文字を読んではいけません。彼らはこのような方法で記憶を保存しようとしていますか? –

答えて

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

ホープ、このことができます。

関連する問題