ArrayList未返回预期数据

时间:2017-12-09 20:13:40

标签: java 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();
}

set和get方法的类:

    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";
  }

}

当我在其for循环之外调用我的ArrayList的get方法时,每个索引处的文本都会被最后一个索引处的文本覆盖。

我认为我在这里缺少一些构造函数和对象的基本概念。

感谢任何帮助。感谢。

2 个答案:

答案 0 :(得分:0)

你的预感是正确的,你错过了emp对象的创建。您必须将emp对象创建移动到循环中。

答案 1 :(得分: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();
    }
    
  3. 希望这有帮助。