gpt4 book ai didi

java - 如何将文件中的输入放入树形图中

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:59 24 4
gpt4 key购买 nike

我试图将读取文件的扫描仪对象中的数据放入树形图中。有什么建议吗?

这里是代码:

   public class StudentViewController {
try {
Scanner ReadFile= new Scanner(new File(currentFileName));
Student.ReadStudent(ReadFile);
studentMap.put



} catch (Exception e) {
System.out.println("Corrupt File");
System.out.print(e);
}
}

这是我读取数据的方法:

   public static Student ReadStudent (Scanner sc){

Student student = new Student(sc.next(),sc.next());
student.ConcreateID = sc.nextInt();
NextID = sc.nextInt();
int gradeNum = sc.nextInt();
//Loop that runs through the file the number of grades specified for that student,
//retrieving each grade and adding it into the array for that specific student Object.
for (int i = 0; i < gradeNum; i++) {
student.addGrade(sc.nextDouble());
}
return student;
}

}

这是 StudentMap 的贴花位置:

public class StudentViewController {
//student version number

private double versionNum = 1.0;
//A boolean to check if the Student file has been altered
private static boolean dirtyByte = false;
//a scanner to take in input data for operations
private Scanner Scan = new Scanner(System.in);
//A tree map to store the students
private TreeMap<String, Student> studentMap = new TreeMap<String, Student>();
//a String to take in input option
String input;
//Name of the current file being written or read
private static String currentFileName = "";

最佳答案

您可以像这样将从 ReadStudent() 方法返回的新创建的 Student 添加到您的 map 中。

studentMap.put("aUniqueStringToIdentifyTheStudent", Student.ReadStudent(ReadFile));

这里需要注意的一点是,您的 try-catch 应该位于 StudentViewController 类中的方法内,而不是像 StudentViewController.

关于java - 如何将文件中的输入放入树形图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20213802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com