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

时间:2013-11-26 10:01:17

标签: java map

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

这是代码:

   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;
}

}

这是学生地图被贴上的地方:

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 = "";

1 个答案:

答案 0 :(得分:0)

您可以将Student方法返回的新创建的ReadStudent()添加到您的地图中。

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

此处需要注意的一点是,try-catch应位于班级StudentViewController内的某个方法内,而不是在StudentViewController中徘徊。