【问题标题】:Copy from text file to Object type arrayList in Java [closed]在Java中从文本文件复制到对象类型arrayList [关闭]
【发布时间】:2014-04-09 17:31:17
【问题描述】:

我正在使用基于控制台的 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, >>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 凯西 工商管理硕士

1112114 约翰 BABS

每个对象都在不同的行

【问题讨论】:

  • 那有什么问题?
  • 你的代码应该做什么?它的实际作用是什么?
  • 我忘记写问题了。现在我已经编辑了它。要是人们在投反对票之前给我们一些新人时间就好了
  • 在第一种情况下,您正在创建 arraylist 类型为 NewStudent 的对象。在第二个中,您只是在创建 arraylist of String。所以显然不会有任何对象,只会打印出字符串
  • @GoldRoger 我在学生 Bean 类的 setName 设置器中使用该字符串

标签: java text arraylist


【解决方案1】:

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

改变

1112149 Alexx BSCS

1112155 塔玛拉 BSCS

1112154 凯西 BBA

1112114 约翰·巴布斯

1112149:Alexx: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();
}

【讨论】:

  • 这是这个结果。 null 1112149, Alexx, BSCSnullnull nullnull 1112155, Tamara, BSCSnullnull nullnull 1112154, Kathy, BBAnullnull nullnull 1112114, John, BABSnull
  • 粘贴你的txt文件的样子...
  • 检查编辑。我已经发布了。请注意,所有对象都在不同的行中
  • 好的,我按照你说的改变了一切,但它仍然给我空的东西。
  • 我没有使用任何 int 值。都是字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
相关资源
最近更新 更多