我正在制作一个管理学生详细信息的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"));
}
代替 :
myArray.get(line)[0]
采用:
myArray.get(indexFile)[0]
因此,在这里您要按索引访问列表元素,然后一旦获得该元素,就尝试从列表中访问学生姓名等。
我建议,您使用学生对象并将编号,名称等作为学生对象的字段,而不是使用数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句