我想从文本文件中查寻我想要的学生资料,要怎么做?我只会写把全部学生的资料都显示出来。。。。
注册的学科要怎么显示全部的?我只能做出来一个。。。。
这是我文本文件中的资料:
学号 姓名 出生日期 性别 国家 电话 入学年份 注册学科
1 wanglei 20/08/1987 男 中国 139xxxxxxxx 2006 cosc2235 cosc2236 cosc2237 cosc2365
2 bingjie 12/12/1986 女 中国 135xxxxxxxx 2004 cosc2238 cosc2658
3 xiaowen 02/03/1986 女 中国 136xxxxxxxx 2005 cosc5486 cosc2562 cosc2589 cosc2456 cosc2589
。
。
。
这是我写的code:
import java.io.*;
import java.util.*;
public class Main{
public static void main (String[] args){
final int MAX = 300;
Student[] student = new Student[MAX];
StringTokenizer tokenizer;
String studentId, studentName, dateOfBirth, sex, nationality, phoneNum;
int yearEnrolled;
String courseCode;
String line, file = "record.txt";
int count = 0;
try{
FileReader fr = new FileReader(file);
BufferedReader inFile = new BufferedReader(fr);
line = inFile.readLine();
while(line != null){
tokenizer = new StringTokenizer(line);
try{
studentId = tokenizer.nextToken();
studentName = tokenizer.nextToken();
dateOfBirth = tokenizer.nextToken();
sex = tokenizer.nextToken();
nationality = tokenizer.nextToken();
phoneNum = tokenizer.nextToken();
yearEnrolled = Integer.parseInt(tokenizer.nextToken());
courseCode = tokenizer.nextToken();
student[count++] = new Student(studentId, studentName, dateOfBirth, sex, nationality, phoneNum,yearEnrolled, courseCode);
}
catch(NumberFormatException exception){
System.out.println("错误");
System.out.println(line);
}
line = inFile.readLine();
}
inFile.close();
for(int scan = 0; scan < count; scan++)
{
System.out.println(student[scan]);
}
}
catch(FileNotFoundException exception){
System.out.println("文件 "+file+" 不存在.");
}
catch(IOException exception){
System.out.println(exception);
}
}
}
public class Student{
private String studentId;
private String studentName;
private String dateOfBirth;
private String sex;
private String nationality;
private String phoneNum;
private int yearEnrolled;
private String courseCode;
public Student(String studentId,String studentName,String dateOfBirth,
String sex, String nationality, String phoneNum,
int yearEnrolled, String courseCode){
this.studentId = studentId;
this.studentName = studentName;
this.dateOfBirth = dateOfBirth;
this.sex = sex;
this.nationality = nationality;
this.phoneNum = phoneNum;
this.yearEnrolled = yearEnrolled;
this.courseCode = courseCode;
}
public String getCourseCode(){
return courseCode;
}
public String getStudentId(){
return studentId;
}
public String getStudentName(){
return studentName;
}
public String getDateOfBirth(){
return dateOfBirth;
}
public String getSex(){
return sex;
}
public String getNationality(){
return nationality;
}
public String getPhoneNum(){
return phoneNum;
}
public int getYearEnrolled(){
return yearEnrolled;
}
public String toString(){
System.out.println();
System.out.println("浏览学生 "+studentId+" 的资料");
System.out.println("==============================");
String student = "Student Id\t\t: "+studentId +"\nStudent Name\t\t: "+studentName
+"\nStudent DOB\t\t: "+dateOfBirth +"\nStudent Sex\t\t: "+sex
+"\nStudent Nationality\t: "+nationality
+"\nStudent PhoneNO.\t: "+phoneNum
+"\nStudent Year Enrolled\t: "+yearEnrolled
+"\nCourseCode\t\t: "+courseCode;
return student;
}
}
拜托各位帮我看下, 多谢~~~