importjava.io.*;importjava.util.*;publicclassTextFileTest{publicstaticvoidmain(String[]args){Employeestaff[]=newEmployee[3];staff[0]=newEmployee("HarryHacker",50000,1989,...
import java.io.*;
import java.util.*;
public class TextFileTest
{
public static void main(String [] args)
{
Employee staff[] = new Employee[3];
staff[0] = new Employee("Harry Hacker",50000,1989,10,1);
staff[1] = new Employee("Carl Cracker",75000,1987,12,15);
staff[2] = new Employee("Tony Tester",40000,1990,3,15);
try
{
PrintWriter out = new PrintWriter(new FileWriter("employee.dat"));
writeData(staff,out);
out.close();
Scanner in = new Scanner(new FileReader("employee.dat"));
//BufferedReader in = new BufferedReader(new FileReader("employee.dat"));
Employee[] newstaff = readData(in);
in.close();
/*for(Employee ex: newstaff)
System.out.println(ex);*/
}
catch(IOException e)
{
e.printStackTrace();
}
}
private static void writeData(Employee[] em, PrintWriter out)throws IOException
{
out.println(em.length);
/*for(Employee e : employee)
e.writeData(out);*/
}
private static Employee[] readData(Scanner in)
{
int n = in.nextInt();
in.nextLine();
//String n = in.readLine();
Employee employees[] = new Employee[n];
for(int i = 0; i
{
employees[i] = new Employee();
employees[i] = readData(in);
}
return employees;
}
}
class Employee
{
private String name;
private double salary;
private Date hireDay;
public Employee()
{}
public Employee(String n,double s,int year,int month,int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);
hireDay = calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
/*public void raiseSalary(double by)
{
double re = salary*by/100;
salary +=re;
}*/
public String toString()
{
return getName()+"[name="+ name +", salary="+salary+", hireDay="+hireDay+"]";
}
public void writeData(PrintWriter out)
{
GregorianCalendar rian = new GregorianCalendar();
rian.setTime(hireDay);
out.println(name +"|"+salary+"|"+rian.get(Calendar.YEAR)+"|"+(rian.get(Calendar.MONTH)+1)+"|"+rian.get(Calendar.DAY_OF_MONTH));
}
public void readData(Scanner in)
{
String line = in.nextLine();
String tokens[] = line.split("\\|");
name = tokens[0];
salary = Double.parseDouble(tokens[1]);
int y = Integer.parseInt(tokens[2]);
int m = Integer.parseInt(tokens[3]);
int d = Integer.parseInt(tokens[4]);
GregorianCalendar fd = new GregorianCalendar(y,m-1,d);
hireDay = fd.getTime();
}
}
编译出来老是出现:不兼容的类型 employees[i] = readData(in);
TextFileTest.java:51: 不兼容的类型
找到: Employee[]
需要: Employee
employees[i] = readData(in);
展开