简体   繁体   中英

How to properly print a 2D array to a text file in Java?

I can get my code to work properly for the most part and to print 3 .csv files into the "SUBMIT" folder with the correct file titles, however I can't figure out how to eliminate the commas at the end of each line of data, nor can I figure out how to write the headerValues onto the second line of the text file in the same comma separated manner without spaces before or after the commas. It's important to note that I have to write to the .csv files with a nested for loop from the 2D array.

import java.io.*;

public class HW01 {
    public static void main(String args[]) throws IOException {

        // Create a 1D array to hold header labels
        String headerLabels[] =  
            {
             "COURSE ID", "TEAM ID", "STUDENT FIRST NAME",
             "STUDENT LAST NAME", "STUDENT ID", "ASSIGNMENT ID",
             "DATE SUBMITTED", "TIME SUBMITTED", "SUBMITTED BY"
            };

        // Create a 2D array to hold header values
        String headerValues[][] =
            {
            {"CMPS280-01", "Winners03", "Jacob", "Harley", "w0389342", "H03", "8/23/2017", "7:24PM", "Jacob Harley"},
            {"CMPS280-02", "Invokers01", "James", "Brown", "w0479045", "H01", "8/25/2017", "1:14PM", "James Brown"},
            {"SE101-02", "CodeIt00", "Keith", "Dillinger", "w0782345", "S04", "8/25/2017", "1:23AM", "Keith Dillinger"}
            };

        // Create an int to hold number of students
        int students = headerValues.length;

        // Loop to name .csv files
        for (int i = 0; i < students; i++){

            // Create new .csv file and store in SUBMIT folder
            String path = "SUBMIT/"+headerValues[i][0]+"_"+headerValues[i][5]+"_"+headerValues[i][1]+"_"+headerValues[i][4]+".csv";
                File file = new File(path);
                PrintWriter writer = new PrintWriter(file);

            // Print headerLabels and headerValues
            // for each student into .csv files using loops
            for (int j = 0; j < headerValues[i].length; j++){
                writer.print(headerLabels[j] + ",");
                writer.print(headerValues[i][j] + ",");
            }
                writer.close();
        }

    }
 }

You need to split your final for loop into 2 separate loops - one for the headers and one for the values. You can fix the comma issue by only printing it when the index j is less than the last index value.

for (int j = 0; j < headerLabels[i].length; j++){
    writer.print(headerLabels[j]);
    if(j<headerLabels[i].length-1) writer.print(",");
}
writer.println();

for (int j = 0; j < headerValues[i].length; j++){
    writer.print(headerValues[i][j]);
    if(j<headerValues[i].length-1) writer.print(",");
}
writer.println();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM