Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+22 votes

I am trying to get the new card pack in the software but I am not exactly sure where to put it and how to reference it from the code. I currently have the data files and images in the project (just in GymnasticsApp, not inside the src folder or anything) but I'm not then sure how to access them from the code. Specifically, I'm not sure how to loop through all of the csv files. Has anyone found a way to do this that works well?

asked in CSC305 Fall 2023 by (1 point)

3 Answers

+11 votes

You can use a folder as a File object just like any other file you may have used. If you supply the path to the folder with the CSVs you can iterate through its contents like an array and call your CSV reading method on them. This stack overflow thread has some examples of this:
https://stackoverflow.com/questions/1844688/how-to-read-all-files-in-a-folder-from-java

answered by (1 point)
+8 votes

Assuming you have imported CSV with OpenCSV libraryt to your project, you can read the CSV files like how my team implemented it:

public class CSVReader {

public static void addCardsFromCSVFile(File csvFile) throws CsvValidationException, IOException {
    // Initialize CSV reader
    CSVReader reader = new CSVReaderBuilder(new FileReader(csvFile)).build();

    // Array to hold each line of the CSV file
    String[] nextLine;

    // Iterate through each line in the CSV file
    boolean isFirstLine = true;
    while ((nextLine = reader.readNext()) != null) {
        // Skip the first line if it contains headers
        if (isFirstLine) {
            isFirstLine = false;
        } else {
            // Create a Card object from the CSV data
            Card card = new Card(nextLine);
            // add it to a data structure
        }
    }
    // Optionally, return the HashMap or perform other actions

}

answered by (1 point)
+5 votes

In theory, you would have it so that as you add them to a specific folder, the software would automatically run it. So check your code and make sure that it is extensible

answered by (1 point)
...