所以这就是我的困境:我有一个文本文件,其中包括学生名单和归因于他们的详细信息,例如他们的学生证,他们的姓名,学期完成了几个小时,他们的专业......等等。以下是文件的外观:
222545767
name
Computer Science
15
60
222123767
name name
Computer Science
12
45
222334111
first last
Information Systems
16
54
622909311
first last
Computer Systems
12
48
222766721
first last.
Telecommunications
45
144
222123456
first last
Mathematics
15
54
现在,当我读入文本文件时,我希望将每个学生分配到一个数组对象。我对如何做到这一点非常模糊,但到目前为止这是我的代码:
public class Student
{
private int id;
private String firstName;
private String lastName;
private String major;
private int hoursCompleted;
private int gradePoints;
Student[] studentsArray = new Student[30];
public Student()
{
}
public Student(int id, String firstName, String lastName,
String major, int hoursCompleted, int gradePoints)
{
}
public void readStudentsfile(){
String fileName = "Students.txt";
Scanner inputStream = null;
try
{
inputStream = new Scanner(new File(fileName));
}
catch (FileNotFoundException e)
{
System.out.println("error");
System.exit(0);
}
while(inputStream.hasNextLine()){
int id = inputStream.nextInt();
String firstName= inputStream.next();
String lastName= inputStream.next();
String major= inputStream.nextLine();
int hoursCompleted= inputStream.nextInt();
int gradePoints= inputStream.nextInt();
studentsArray[1] = new Student(id, firstName, lastName, major, hoursCompleted, gradePoints);
// this part is not correct but is where I need to assign
// all of the variables read from the file into array objects
}
inputStream.close();
}