特别是从一系列课程写入csv文件

时间:2014-03-23 20:08:05

标签: java arrays io

我在使用我编写的类中的方法时遇到了问题。以下方法添加到我创建的名为Course的类中。目标是将对象的所有属性作为String写在一行上。

// 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()

好的,所以在将该方法添加到Class之后。然后我开始创建一个从Course数组写入CSV文件所需的方法(从main方法调用),该文件调用上述方法。这是我写的方法。

// 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找到toCSVString方法,该方法是在Course类中的toString方法之后定义的。最初代码中的那一行看起来像这样:

outfile.write(toCSVString(courseArray[i]));

但是我的IDE无法找到它所以我在它前面添加了课程对象的实例。但是,我仍然遇到麻烦。

有人看到我做错了吗?

编辑#1

这是我的课程中的课程课程。我遇到了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

1 个答案:

答案 0 :(得分:1)

你有:

outfile.write(courseArray.toCSVString(courseArray[i]));

你的意思是:

outfile.write(courseArray[i].toCSVString(courseArray[i]));

由于toCSVStringCourse的成员,而不是Course[]的成员(courseArrayCourse[],而您正试图致电.toCSVString()在数组本身,这是无效的。)

另请注意,在此表单中,将Course作为参数传递是多余的,因为您没有使用它,并且您还希望this而不是其他Course无论如何。我建议完全抛弃该参数(因为它未被使用):

public String toCSVString () {      // <- c wasn't actually used
    String record = campus + ","    // <- and this. is implied here
                  + course + ","
                  + section + ","
                  + crn + ","
                  + credits + ","
                  + time + ","
                  + days + "\n";
    return record;
}

你只需将其称为:

outfile.write(courseArray[i].toCSVString());

或者,如果您愿意,可以使用方法static并使用参数(尽管在这种情况下这不会给您带来任何特别的好处):

public static String toCSVString (Course c) {
    String record = c.campus + "," 
                  + c.course + ","
                  + c.section + ","
                  + c.crn + ","
                  + c.credits + ","
                  + c.time + ","
                  + c.days + "\n";
    return record;
}

如果选择静态方法,则将其称为:

outfile.write(Course.toCSVString(courseArray[i]));