从文本文件复制到Java中的对象类型arrayList

时间:2014-04-09 17:31:17

标签: java text arraylist

我正在使用基于控制台的java制作学生系统。我正在尝试将文本文件的内容加载到ArrayList,然后尝试使用学生注册号进行搜索,并列出他/她的名字和程序。 我很难将文本文件内容加载到ArrayList中,以便稍后可以将它们作为对象进行检索。

我已经创建了一个在Arraylist上完美运行的搜索功能(因为在输入新记录时我将它们放在ArrayList中,可以搜索它们直到程序没有关闭)

这是我尝试在ArrayList中加载文本文件的方法:

try {
    ArrayList<NewStudent> student = new ArrayList<>(); 
    BufferedReader  inFile = new BufferedReader (new FileReader ("Test.txt"));
    String inputLine;

    while ((inputLine = inFile.readLine())!=null) { 
        NewStudent ns = new NewStudent();
        String[] studentVars = inputLine.split(":"); 
        ns.setName(studentVars[0]);

        student.add(ns);
    }
    System.out.print(student);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

这是我的搜索方法,这就像魅力一样

public Student Search(String ID, ArrayList<Student> StudentList) {
    Student myStudent = new Student();

    for(int i=0 ; i<StudentList.size() ; i++) {
        if(StudentList.get(i).getID().equals(ID)) {
            myStudent.setID(StudentList.get(i).getID());
            myStudent.setName(StudentList.get(i).getName());
            myStudent.setProgram(StudentList.get(i).getProgram());

            return myStudent;
        }
    }
    return null;
}

打印时,会打印出来:

  
    

[com.Student.NewStudent@615e7597,com.Student.NewStudent @ 7a3e72,com.Student.NewStudent @ 5999ae9c,&gt;&gt; com.Student.NewStudent@7896b1b8,com.Student.NewStudent@6d6de4e1,com。 Student.NewStudent@49cda7e7,com.Student.NewStudent@5cca548b]

  

如果我使用不同的方法:

BufferedReader inFile = new BufferedReader (new FileReader ("Student.txt"));
ArrayList<String> arrayList = new ArrayList<>();
String inputLine;
while ( (inputLine = inFile.readLine() ) != null) {
    String[] stud = inputLine.split(":"); 
    arrayList.add(stud[0]); \\if I add stud[1] it gives null pointer exception
}
System.out.println(arrayList);

这不会作为单独的对象返回。它显示了一条直线

这就是我的文字文件的样子

1112149 阿里克斯 BSCS

1112155 塔玛拉 BSCS

1112154 凯西 BBA

1112114 约翰 BABS

每个对象都在不同的行

1 个答案:

答案 0 :(得分:1)

编辑:你试图拆分:,但我没有看到任何:在文本文件中?

更改

1112149 Alexx BSCS

1112155 Tamara BSCS

1112154 Kathy BBA

1112114 John BABS

1112149:阿里克斯:BSCS

1112155:塔玛拉:BSCS

1112154:卡西:BBA

1112114:约翰:BABS

然后使用以下代码:

try {

   ArrayList<NewStudent> student = new ArrayList<>(); 
   BufferedReader inFile = new BufferedReader (new FileReader("Test.txt"));
   String inputLine;

   while ((inputLine = inFile.readLine())!=null) { 

      if(inputLine.isEmpty()) continue; //i dont know if you have blank lines between students in txt, if so. use this line of code.

      NewStudent ns = new NewStudent();
      String[] studentVars = inputLine.split(":"); 
      ns.setId(studentVars[0]);
      ns.setName(studentVars[1]);
      ns.setProgram(studentVars[2]);

      System.out.println("ID: " + studentVars[0] + " Name: " + studentVars[1] + " Program: " + studentVars[2])

      student.add(ns);
}
   //or you could use this to loop and print all items in Arraylist.
   for(NewStudent nStudent : student) {
      System.out.println(nStudent.getId() + " " + nStudent.getName() + " " + nStudent.getProgram());
   }
} catch (FileNotFoundException e) {
   e.printStackTrace();
}