【发布时间】:2014-05-01 01:02:20
【问题描述】:
我在使用我编写的类中的方法时遇到问题。以下方法已添加到我创建的名为 Course 的类中。目标是将对象的所有属性写在一行上作为字符串。
// method to return properties as a CSV string on one line
public String toCSVString(Course c) {
String record = c.campus + ","
+ c.course + ","
+ c.section + ","
+ c.crn + ","
+ c.credits + ","
+ c.time + ","
+ c.days + "\n";
return record;
} //end toCSVString()
好的,所以在将该方法添加到类之后。然后我开始创建一个方法(从 main 方法调用)需要从 Course 数组写入调用上述方法的 CSV 文件。这是我写的方法。
// add a method here to write from an array of courses to a CSV file
public static void writeCSV(Course[] courseArray, int count) throws Exception {
//create a File class object and give the file the name employees.csv
java.io.File courseCSV = new java.io.File("courses.csv");
//Create a Printwriter text output stream and link it to the CSV File
java.io.PrintWriter outfile = new java.io.PrintWriter(courseCSV);
//Iterate the elements actually being used
for (int i=0; i < count ; i++) {
outfile.write(courseArray.toCSVString(courseArray[i]));
}//end for
outfile.close();
} //end writeCSV()
我遇到了以“outfile.write”开头的行
在我的代码中,我无法让 Netbeans 找到在 Course 类中的 toString 方法之后定义的 toCSVString 方法。最初代码中的那一行是这样的:
outfile.write(toCSVString(courseArray[i]));
但是我的 IDE 找不到它,所以我在它前面添加了课程对象的实例。但是,我仍然遇到问题。
有人看到我做错了吗?
编辑 #1
这是我程序中的 Course 课程。我在使用 toCSVString 方法时遇到问题。
class Course implements Serializable {
private String campus; // the campus on which the course is offered
private String course; // the course number, such as CSCI 111
private String section; // the section number
private String crn; // the CRN for this section
private int credits; // the number od credits for the course
private String time; // the time the course is offered, such as 8:00 to 10:00 A.M.
private String days; // the Days the course is offered, suhc as MW
// constructors
Course() {
}
Course(String course, String section, String crn, int credits) {
this.course = course;
this.section = section;
this.crn = crn;
this.credits = credits;
} // end Course() initalizing
// muatator methods
public void setCampus(String cmp) {
this.campus = cmp;
}// end setCampus()
public void setCourse(String crse) {
this.course = crse;
}// end setCourse()
public void setSection(String sect) {
this.section = sect;
} // end setSection()
public void setCRN(String crn) {
this.crn = crn;
} // end setCRN()
public void setCredits(int cr) {
this.credits = cr;
} // end setCredits()
public void setTime(String tm) {
this.time = tm;
}// end setTime()
public void setDays(String days) {
this.days = days;
}// end setDays()
// accessor methods
public String getCampus() {
return campus;
} // end getCampus()
public String getCourse() {
return course;
} // end Course()
public String getSection() {
return section;
} // end getSection()
public String getCRN() {
return crn;
} // end getCRN()
public int getCredits() {
return credits;
} // end getCredits()
public String getTime() {
return time;
} // end getTime()
public String getDays() {
return days;
} // end getDays()
// method to compare by CRN using the String class compareTo()
public int compareTo(Course other) {
return this.crn.compareTo(other.getCRN());
} // end compareTO()
// method to return properties as a string
public String toString() {
return campus + " "
+ course + " "
+ section + " "
+ crn + " "
+ credits + " "
+ time + " "
+ days;
} // end toString()
// method to return properties as a CSV string on one line
//public String toCSVString(Course c){
public String toCSVString (Course c){
String record = campus + ","
+ course + ","
+ section + ","
+ crn + ","
+ credits + ","
+ time + ","
+ days + "\n";
return record;
} //end toCSVString()
}// end class Course
【问题讨论】:
-
请向我们展示整个班级(或至少是大纲)。
toCSVString()真的是Course的成员吗? -
@JasonC 我自己将方法添加到课程中。我将更新上面显示课程类的问题。
-
您想打印
courseArray中的所有courses吗?如果是这样,您就不需要count变量。此外,如果您constructor为空,则可以跳过声明。