为每行文本文件创建对象

时间:2015-02-21 00:33:00

标签: java object constructor

我试图做一些相对简单的事情;逐行读入文本文件,然后为每一行创建一个'Course'对象。然后,我想将每个对象添加到将在实例方法中返回的新'CourseList'对象。

这是我到目前为止所做的:(我只是将我的尝试部分包含在方法中)

try {

  FileReader reader = new FileReader(inputName);
  BufferedReader inFile = new BufferedReader(reader);

  String nextLine = inFile.readLine();
  Course newObject = new Course();
  CourseListQ2 newList = new CourseListQ2();//entire object to be returned

  while(nextLine!=null){
    newList.addCourse(newObject);
  } 
  return newList;

  inFile.close();
}

我收到Course newObject = new Course();行的错误消息:

错误:构造函数课程中的课程不能应用于给定的类型;   required:java.lang.String,java.lang.String,int

所以我不认为我理解如何为每一行创建一个Course对象。甚至是真正的意义。有什么建议吗?

4 个答案:

答案 0 :(得分:2)

您的代码中的修正很少:

Course newObject = new Course();//constructor requires 3 params of String, String and int, what you get as compile time error.
CourseListQ2 newList = new CourseListQ2();
while(nextLine!=null){//you are not reading the next line, so it will be a infinite loop after reading the first line of file
    newList.addCourse(newObject);//adding same course object instead you need different courses from each line
} 
return newList;//must be after the below line
inFile.close();//in future compiler is gona give error for writing below return keyword

被修改 您可以尝试以下代码:

CourseListQ2 newList = new CourseListQ2();
BufferedReader inFile = null;
try {
  inFile = new BufferedReader(new FileReader(inputName));
  String nextLine;
  String param1;
  String param1;
  int param3;
  while(null != (nextLine = inFile.readLine())){//read line by line
    param1 = your code here to play with nextLine
    param2 = your code here to play with nextLine
    param3 = your code here to play with nextLine
    newList.addCourse(new Course(param1, param2, param3));
  }
} catch(IOException ex) {
   ex.printStackTrace(System.err);//print exception on console
} finally {//its optional but recommended
  if(null != inFile) {
     try {
        inFile.close();//may cause trouble
     } catch(IOException ex) {
        ex.printStackTrace(System.err);//print exception on console
     }
  }
  return newList;
}

答案 1 :(得分:1)

Course对象的构造函数需要三个参数:String,String和int。

您可以提供这些参数,也可以将不带参数的构造函数添加到Course类中。

答案 2 :(得分:1)

如果你需要为每一行添加一个新的课程对象,那么你必须移动一些东西。

try {

  FileReader reader = new FileReader(inputName);
  BufferedReader inFile = new BufferedReader(reader);

  String nextLine = inFile.readLine();
  Course newObject;
  CourseListQ2 newList = new CourseListQ2();//entire object to be returned

  while(nextLine!=null){
    newObject = new Course(); //There are paramaters that the constructor                
                              //requires that you need to include
    newList.addCourse(newObject);
  } 
  return newList;

  inFile.close();
}

除了将对象创建移动到while循环中(因此为每一行创建一个新对象)之外,还需要更正该行

newObject = new Course();

根据您的错误,您尝试使用不退出的构造函数创建对象。如果查看Course类,您会发现没有任何参数的构造函数。您有两种选择:添加不带参数的构造函数,或更改上面的行以调用其他构造函数。

newObject = new Course(String, int, int);

答案 3 :(得分:1)

检查你的默认构造函数,你是否链接使用另一个构造函数,比如this(someparam,someparam,someparam)。 从默认构造函数传递的参数与其他构造函数的参数

不匹配