为每一行文本文件创建对象
Create object for each line of text file
我正在尝试做一些相对简单的事情;逐行读取文本文件,然后为每一行创建一个 '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();
行的错误消息:
错误:class 中的构造函数课程无法应用于给定类型;
要求:java.lang.String,java.lang.String,int
所以我不认为我理解如何为每一行创建一个 Course
对象。甚至那真正意味着什么。有什么建议吗?
课程对象的构造函数需要三个参数:String、String 和 int。
您可以提供这些参数或将不带参数的构造函数添加到您的课程 class。
如果您需要为每一行添加一个新的课程对象,那么您必须移动一些东西。
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();
根据您的错误,您正在尝试使用不退出的构造函数创建对象。如果您查看您的课程 class,您会发现没有不带任何参数的构造函数。您有两个选择:添加一个不带参数的构造函数,或者更改上面的行以调用不同的构造函数。
newObject = new Course(String, int, int);
检查您的默认构造函数,您是否链接使用另一个构造函数,使用类似 this(someparam, someparam, someparam) 的东西。
从您的默认构造函数传递的参数与您的其他构造函数之一的参数不匹配
您的代码中有几处更正:
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;
}
我正在尝试做一些相对简单的事情;逐行读取文本文件,然后为每一行创建一个 '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();
行的错误消息:
错误:class 中的构造函数课程无法应用于给定类型; 要求:java.lang.String,java.lang.String,int
所以我不认为我理解如何为每一行创建一个 Course
对象。甚至那真正意味着什么。有什么建议吗?
课程对象的构造函数需要三个参数:String、String 和 int。
您可以提供这些参数或将不带参数的构造函数添加到您的课程 class。
如果您需要为每一行添加一个新的课程对象,那么您必须移动一些东西。
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();
根据您的错误,您正在尝试使用不退出的构造函数创建对象。如果您查看您的课程 class,您会发现没有不带任何参数的构造函数。您有两个选择:添加一个不带参数的构造函数,或者更改上面的行以调用不同的构造函数。
newObject = new Course(String, int, int);
检查您的默认构造函数,您是否链接使用另一个构造函数,使用类似 this(someparam, someparam, someparam) 的东西。 从您的默认构造函数传递的参数与您的其他构造函数之一的参数不匹配
您的代码中有几处更正:
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;
}