【问题标题】:Specifically writing from an array of courses to a csv file专门从课程数组写入 csv 文件
【发布时间】: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 为空,则可以跳过声明。

标签: java arrays io


【解决方案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]));

【讨论】:

  • 这回答了我的问题。非常感谢您的深入回复。感激不尽。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 2023-03-31
  • 2018-03-08
相关资源
最近更新 更多