我正在制作一个管理学生详细信息的Java程序,并希望在修改和显示学生详细信息方面提供一些帮助。
这是我为搜索学生所做的代码,并考虑过将代码回收到修改和显示学生,但是,我被卡住了。
public static void searchStudent(){
try{
BufferedReader readFile = new BufferedReader(new FileReader(file));
System.out.println("--- Searching for a Student ---");
System.out.print("Enter Student Number, Last Name or First Name: ");
String option = sc.nextLine();
System.out.println("Searching for '" + option + "' in the Student File");
System.out.print("\n");
while((line = readFile.readLine()) != null){
int indexFile = line.indexOf(option);
if(indexFile > -1){
System.out.println(line);
System.out.print("\n");
}
}
readFile.close();
} catch(IOException ioe){
System.out.println("Error Searching for a Student !!!");
}
}
这是我显示学生的代码,有关如何解决它的任何提示?提前致谢
public static void displayStudent(){
try{
BufferedReader readFile = new BufferedReader(new FileReader(file));
System.out.println("--- Displaying Student Details ---");
System.out.print("Enter Student Number: ");
String option = sc.nextLine();
System.out.println("Searching for Student '" + option + "' in the Student File");
System.out.print("\n");
while((line = readFile.readLine()) != null){
int indexFile = line.indexOf(option);
if(indexFile > -1){
System.out.println("Student Number: " + myArray.get(line)[0]);
System.out.println("Last Name: " + myArray.get(line)[1]);
System.out.println("First Name: " + myArray.get(line)[2]);
System.out.println("Email Address: " + myArray.get(line)[3]);
System.out.println("Mobile Number: " + myArray.get(line)[4]);
}
}
readFile.close();
} catch(IOException ioe){
System.out.println("Error Displaying a Student !!!");
}
}
ArrayList myArray填充为:
for(int x = 0; x < myArray.size(); x++){
bw.write(myArray.get(x)[0] + ";");
bw.write(myArray.get(x)[1] + ";");
bw.write(myArray.get(x)[2] + ";");
bw.write(myArray.get(x)[3] + ";");
bw.write(myArray.get(x)[4] + ";");
bw.write(System.getProperty("line.separator"));
}
答案 0 :(得分:0)
而不是:
myArray.get(line)[0]
使用:
myArray.get(indexFile)[0]
所以在这里你要按索引访问列表元素,然后一旦你得到元素,你就试着从列表中访问学生姓名等。
我建议你使用Student Object并将number,name等作为Student对象的字段而不是使用Array。