ArrayList not returning expected data

I don’t know what I’m doing and would appreciate any help. I’m reading a text file with the following code:

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

class for set and get methods:

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

}

When I call the get method of my ArrayList outside of its for loop, the text at each index is overwritten by the text at the last index.

I think I’m missing some fundamental concept of constructors and objects here.

Any help is appreciated. Thanks.

Answer

Your hunch is correct, you are missing the emp object creation. You have to move the emp object creation into the loop.

Leave a Reply

Your email address will not be published. Required fields are marked *