Yotta Lynn
Yotta Lynn

Reputation: 41

Please pay attention to the FILENAME and how to print out the filename that I choose from my computer?

it is a simple question, how to print out the selected file name, thanks.

public class CSVMAX {
public CSVRecord hottestInManyDays() {
//select many csv files from my computer
   DirectoryResource dr = new DirectoryResource();
   CSVRecord largestSoFar = null;
//read every row and implement  the method we just define
   for(File f : dr.selectedFiles()) {
    FileResource fr = new FileResource(f);
    CSVRecord currentRow = hottestHourInFile(fr.getCSVParser());
    if (largestSoFar == null) {
       largestSoFar = currentRow;
    }
    else {
    double currentTemp = Double.parseDouble(currentRow.get("TemperatureF"));
        double largestTemp = Double.parseDouble(largestSoFar.get("TemperatureF"));
        //Check if currentRow’s temperature > largestSoFar’s
    if (currentTemp > largestTemp) {
                //If so update largestSoFar to currentRow
            largestSoFar = currentRow;
    }
   }
}
return largestSoFar;        
}

here I want to print out the file name but I dont know how to do that.

public void testHottestInManyDay () {
    CSVRecord largest = hottestInManyDays();
    System.out.println("hottest temperature on that day was in file " + ***FILENAME*** + largest.get("TemperatureF") +
               " at " + largest.get("TimeEST"));
}
}

Upvotes: 0

Views: 1297

Answers (3)

Joop Eggen
Joop Eggen

Reputation: 109557

One has to return both the CSVRecord and the File. Either in a newly made class. As CSVRecord can be converted to a map, add the file name to the map, using a new column name, here "FILENAME."

public Map<String, String> hottestInManyDays() {
    //select many csv files from my computer
    DirectoryResource dr = new DirectoryResource();
    CSVRecord largestSoFar = null;
    File fileOfLargestSoFar = null;
    //read every row and implement  the method we just define
    for (File f : dr.selectedFiles()) {
        FileResource fr = new FileResource(f);
        CSVRecord currentRow = hottestHourInFile(fr.getCSVParser());
        if (largestSoFar == null) {
            largestSoFar = currentRow;
            fileOfLargestSoFar = f;
        }
        else {
            double currentTemp = Double.parseDouble(currentRow.get("TemperatureF"));
            double largestTemp = Double.parseDouble(largestSoFar.get("TemperatureF"));
            //Check if currentRow’s temperature > largestSoFar’s
            if (currentTemp > largestTemp) {
                //If so update largestSoFar to currentRow
                largestSoFar = currentRow;
                fileOfLargestSoFar = f;
            }
        }
    }
    Map<String, String> map = new HashMap<>(largestSoFar.toMap());
    map.put("FILENAME", fileOfLargestSoFar.getPath());
    return map;
}

Map<String, String> largest = hottestInManyDays();
System.out.println("hottest temperature on that day was in file " 
        + largest.get("FILENAME") + largest.get("TemperatureF") +
           " at " + largest.get("TimeEST"));

Upvotes: 1

David
David

Reputation: 218847

Ultimately, it seems that hottestInManyDays() will need to return this information.

Does CSVRecord have a property for that?

Something like this:

CSVRecord currentRow = hottestHourInFile(fr.getCSVParser());
currentRow.setFileName(f.getName());

If not, can such a property be added to it?

Maybe CSVRecord doesn't have that property. But it can be added?:

private String _fileName;

public void setFileName(String fileName) {
    this._fileName = fileName;
}

public String getFileName() {
    return this._fileName;
}

If not, can you create a wrapper class for both pieces of information?

If you can't modify CSVRecord and it doesn't have a place for the information you want, wrap it in a class which does. Something as simple as this:

class CSVWrapper {

    private CSVRecord _csvRecord;
    private String _fileName;

    // getters and setters for the above
    // maybe also a constructor?  make them final?  your call

}

Then return that from hottestInManyDays() instead of a CSVRecord. Something like this:

CSVWrapper csvWrapper = new csvWrapper();
csvWrapper.setCSVRecord(currentRow);
csvWrapper.setFileName(f.getName());

Changing the method signature and return value as needed, of course.


However you do it, once it's on the return value from hottestInManyDays() you can use it in the method which consumes that:

CSVWrapper largest = hottestInManyDays();
System.out.println("hottest temperature on that day was in file " + largest.getFileName() + largest.getCSVRecord().get("TemperatureF") +
           " at " + largest.getCSVRecord().get("TimeEST"));

(Note: If the bits at the very end there don't sit right as a Law Of Demeter violation, then feel free to extend the wrapper to include pass-thru operations as needed. Maybe even have it share a common interface with CSVRecord so it can be used as a drop-in replacement for one as needed elsewhere in the system.)

Upvotes: 2

Debosmit Ray
Debosmit Ray

Reputation: 5403

Referring to the line for(File f : dr.selectedFiles())

f is a [File]. It has a toString() method [from docs],

Returns the pathname string of this abstract pathname. This is just the string returned by the getPath() method.

So, in the first line inside the loop, you can put System.out.println(f.toString()); to print out the file path.

Hope this helps clear a part of the story.

Now, to update this string, I see you are using some object that is called largest in testHottestInManyDay(). You should add a filepath string in this object and set it inside the else block.

Upvotes: 1

Related Questions