我在练Java和寻找练习在线:阅读文件到数组 - Java的
不过,我停留在点上,我需要
Read the file again, and initialise the elements of the array
任务
- 写作班成员列表成员列表
- 构造应采取字符串参数(文件名)
- 用扫描仪读取线和创建数组大到足以容纳的文件
- 再次读取该文件,并初始化数组
当前的元素代码
import java.io.*;
import java.util.*;
class Members {
MemberElement[] members;
public Members(String fileName) throws IOException {
File myFile = new File(fileName);
Scanner scan = new Scanner(myFile);
int numOfLines = 0;
while(scan.hasNextLine()) {
scan.nextLine();
numOfLines++;
}
scan.close();
scan = new Scanner(myFile);
members = new MemberElement[numOfLines];
}
MemberElement类:
class MemberElement {
private String name;
private int number;
private int birthDate;
public MemberElement(String name, int number, int birthDate) {
this.name = name;
this.number = number;
this.birthDate = birthDate;
}
public String getName() {
return this.name;
}
public int getNumber() {
return this.number;
}
public int getBirth() {
return this.birthDate;
}
public String toString() {
return getName() + " " + getNumber() + " " + getBirth();
}
}
内容的文本文件:
Wendy Miller 7654 17-2-1960
Dolly Sheep 4129 15-5-1954
Dolly Sheep 5132 21-12-1981
Irma Retired Programmer 345 15-11-1946
他们问你读取文件两次吗?嗯 –
是的,首次读取行数,然后关闭扫描仪。然后再次打开扫描仪以启动阵列 – RandomMath
好的,您的具体问题在哪里?第二次打开文件,还是读取数组中的行? – fxnn