zhughes3
zhughes3

Reputation: 477

Writing an array to a CSV File - Java

I have 24 elements in an array I am looking to write to a CSV file to be opened in Excel.

When I run my program, it creates a CSV file in my Netbeans project folder. However, when I open the CSV file - it is blank.

In my main method, I show the user the array and then call the writeCSV method as shown below:

    //show the user the sorted array
    System.out.println( "The sorted array is: ");
    for ( int i=0; i<24; i++)      
        System.out.println( "\t" + course2[i].toString() );

    //write the data from the duplicate array to a CSV file
    System.out.println("\nWriting data from Course array to CSV File.");
    writeCSV(course2, count);

The writeCSV method is pasted below:

    //write from duplicate 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[i].toCSVString());

    }//end for

    outfile.close();
    } //end writeCSV()

The writeCSV method above calls the toCSVString method which is defined in a class I created named Course. I pasted this method below:

// method to return properties as a CSV string on one line
//public String toCSVString(Course c){
public String toCSVString() {
    String record = campus + ","
                  + course + ","
                  + section + ","
                  + crn + ","
                  + credits + ","
                  + time + ","
                  + days + "\n";

    return record;
} //end toCSVString()

My code runs flawlessly up until I have to write the array to a CSV file. This is when it creates the blank CSV file. This makes me believe I have an error within my toCSVString method or the writeCSV method I believe. Any tips or help would be greatly appreciated. Thanks.

Upvotes: 1

Views: 7785

Answers (1)

Ryan J
Ryan J

Reputation: 8323

For those who just tuned in...

Change your writeCSV method to this:

//write from duplicate array of courses to a CSV file
public static void writeCSV(Course[] courseArray) 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 < courseArray.length ; i++) {
        outfile.write(courseArray[i].toCSVString());

    }//end for

    outfile.close();
} //end writeCSV()
  1. Remove the count argument from this function, unless you actually intend to write a different number of elements into the CSV file. Judging from your main method, this isn't the case.

  2. Change count in your for-loop to courseArray.length

Then, in your main method, change the call to:

writeCSV(course2);

Always be sure to initialize your variables, and when in doubt, make use of your debugger. That could have helped you spot this.

Hope this helps.

Upvotes: 1

Related Questions