将导入文档中的内容添加到arraylist

时间:2014-06-12 10:31:25

标签: java arraylist

我刚刚完成大学第一年的java新手,这是我对stackoverflow的第一次查询,但我已经在这几天了。 我想从txt文档中导入名称,地址和工资,这样工作正常,但是一旦我将它添加到arraylist并尝试将其打印出来,我就会读错了。

这是我的导入阅读器代码:

import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.io.IOException;
import java.io.FileReader;

public class ReadFile{

public static void main(String[] args) throws IOException{
ArrayList<Employee> peopleList = new ArrayList<Employee>();

  //Begiining of document import
  try {
  File myFile = new File("people.txt");
  FileReader fileReader = new FileReader(myFile);

  BufferedReader bReader = new BufferedReader(fileReader);


  String line = null;
  while((line = bReader.readLine()) != null){
  String[] result = line.split(";");

  String name = result[0];
  int age = Integer.parseInt(result[1]);
  double salary = Double.parseDouble(result[2]);

  peopleList.add(new Employee(name,age,salary));

  for (Employee token:peopleList)
  {

     System.out.println(peopleList);
  }
  }
  bReader.close();

  }catch(Exception e){
  e.printStackTrace();
  }
 }
}

以下是people.txt的内容:

Jimmy;32;32000
Paul;28;28000
John;34;45234
Mike;19;19234

这里是Employee类,它是Person的子类:

import java.util.*;
public class Employee extends Person implements Comparable<Employee>, PartTimeAble
{

// variables for the Employee class
protected int idNumber;
// ID Number generator
protected static int nextIdNumber = 001;
private double salary = 0.00;
public double extra;

// Constructors for the Employee class
Employee(String name, int age, double salary) 
{
  // Constructors for the Employee Class 
  // call superclass form Person class
  super(name, age);
  idNumber = nextIdNumber++;
  this.salary = salary;
}

// Get Salary method
public double getSalary() 
{
   return salary;
}

// Get ID Number method
public int getIdNumber()
{
   return idNumber;
}

// Compare to method Overriding generic Compare 
public int compareTo(Employee other)
{
   //compare Result for salary
  if(salary - other.salary == 0)
  {
     return 0;
  }
  else if(salary - other.salary < 0)
  {
     return -1;
  }
  else
     return 1;
}

// Job method for Employee taken from ParTimeAble Interface
// Works out extra salary for Employee after part time salary
public void doJob(Job j)
{
   extra = (j.getPrice()-(j.getPrice()/100*60));

}

public String getDescription()
{
   return "Employee, ID:" + idNumber + "\tName: " + super.getName() + " Age: " +          super.getAge() + "\tSalary: €" + salary + "\tExtra Fee: €" + Math.round(extra);
 }
}

这是Person类:

public abstract class Person

{
 // variables for the Person class
 protected String employeeName = "Don't know";
 protected int employeeAge = 0;

 // Constructors for the Person class
 public Person(String name, int age)
{
   employeeName = name;
   employeeAge = age;
}

 // Get Name
 public String getName(){
  return employeeName;
}

// Get Age
public int getAge(){
  return employeeAge;
}

// Get Description
public abstract String getDescription();

}

这是编译后的输出:

Employee@e86da0
Employee@e86da0
Employee@1754ad2
Employee@e86da0
Employee@1754ad2
Employee@1833955
Employee@e86da0
Employee@1754ad2
Employee@1833955
Employee@291aff

如果有人可以提供帮助,请提前感谢您。

问候。

1 个答案:

答案 0 :(得分:2)

方法System.out.println()调用传递给它的对象的toString()方法。因此默认情况下它会输出class_name @ address_in_memory。您需要覆盖toString()课程的Employee方法。在Employee类内:

@Override
public toString(){
return "Employee, ID:" + idNumber + "\nName: " + super.getName() + "\nAge: " + super.getAge() + "\nSalary: €" + salary + "\nExtra Fee: €" + Math.round(extra);
}

(我认为这是你要打印的?)

相关问题