3

I tried to go with a simple fix to my issue for printing the array to the .csv file and replacing certain characters, however I noticed later that I am not supposed to use any spaces before or after the commas in my output.
I can't use replace or else all of the header labels will have zero spaces either, so I need help with getting the values directly from my nested for loops.

import java.io.*;
import java.util.Arrays;

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-02", "Invokers01", "James", "Brown", "w0479045", "H01", "8/25/2017", "1:14PM", "James Brown"},
            {"CMPS280-01", "Winners03", "Jacob", "Harley", "w0389342", "H03", "8/23/2017", "7:24PM", "Jacob Harley"},
            {"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;

        // Array loop to create student .csv files
        for (int i = 0; i < headerValues.length; i++){
            for (int j = 0; j < students; j++){

            // 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
                writer.println(Arrays.toString(headerLabels).replace("[", "").replace("]", "").replace(" , " , ","));
                writer.println(Arrays.toString(headerValues[i]).replace("[", "").replace("]", "").replace("  ,  ", ","));
                writer.close();
            }
        }

    }
 }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
simplest
  • 217
  • 1
  • 5
  • 16
  • I can tell you first that `Arrays.toString()` is not what you want to do – OneCricketeer Aug 26 '17 at 21:01
  • If you want to join an array on commas, see https://stackoverflow.com/questions/1515437/java-function-for-arrays-like-phps-join#1515450 – OneCricketeer Aug 26 '17 at 21:05
  • I figured out pretty quickly that the Arrays.toString() will never be able to print properly, but instead of joining the Arrays on commas, I would prefer using the nested for loop and my mind is a bit foggy on the syntax of printing the array elements and not the string itself. – simplest Aug 26 '17 at 21:11
  • `String.join()` uses a loop internally. Why do you want to write your own? – OneCricketeer Aug 26 '17 at 21:12
  • By the way, I would really suggest just using Apache commons CSV library. For example. the JavaDoc for the Writer https://commons.apache.org/proper/commons-csv/archives/1.4/apidocs/index.html – OneCricketeer Aug 26 '17 at 21:15
  • The loop is required for my assignment. This is for one of my classes and I have to use a loop in order to print out the information. – simplest Aug 26 '17 at 22:03

1 Answers1

0

The nested loop isn't exactly the problem. The order in which you've tried to write the contents is. You only want 1 file for all the students, I assume, not i*j files. One for each student.

For example,

    // Create student .csv files and store in SUBMIT folder
    String path = "SUBMIT/students.csv";
    try (PrintWriter writer = new PrintWriter(new File(path))) {

        // Print header row
        writer.println(String.join(",", headerLabels));

        // Print all data rows
        for (String[] studentData : headerValues){
            writer.println(String.join(",", studentData);
        }
     }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245