dimanche 29 août 2021

What is the proper way to use if/else statements in my code to check CSV records that equal 'N/A'? [duplicate]

Some of the CSV's contain rare cases of data that I need to create cases for. I'm parsing the string values to doubles to perform checks but some files contain 'N/A' in the record. How do I correctly perform the check so that it doesn't Double.parseDouble a string. That is the error I'm getting.

public CSVRecord lowestHumidityInFile(CSVParser parser) {
    CSVRecord lowestSoFar = null;
    for (CSVRecord record : parser) { 
        if (lowestSoFar == null) {
            lowestSoFar = record;
        }
        //Otherwise
        else if (record.get("Humidity") != "N/A") {
            double currentHum = Double.parseDouble(record.get("Humidity"));
            double lowestHum = Double.parseDouble(lowestSoFar.get("Humidity"));
            if (currentHum < lowestHum) {
                lowestSoFar = record;
            }
        }
    }
    return lowestSoFar;
}
public CSVRecord lowestHumidityInManyFiles() {
    CSVRecord lowestSoFar = null;
    DirectoryResource dr = new DirectoryResource();
    for (File f : dr.selectedFiles()) {
        FileResource fr = new FileResource(f);
        CSVRecord currentHum = lowestHumidityInFile(fr.getCSVParser());
        if (lowestSoFar == null) {
            lowestSoFar = currentHum;
        }
        else if (currentHum.get("Humidity") != "N/A") {
            double currentHumidity = Double.parseDouble(currentHum.get("Humidity"));
            double lowestHumidity = Double.parseDouble(lowestSoFar.get("Humidity"));
            if (currentHumidity < lowestHumidity) {
                lowestSoFar = currentHum;
            }  
        }
    }
    return lowestSoFar;
}

public void testLowestHumidityInFile() {
    FileResource fr = new FileResource();
    CSVRecord lowest = lowestHumidityInFile(fr.getCSVParser());
    System.out.println("Lowest Humidity was " + lowest.get("Humidity") + " on " + lowest.get("DateUTC"));

}

public void testLowestHumidityInManyFiles() {

    CSVRecord lowestFiles = lowestHumidityInManyFiles();
    System.out.println("Lowest Humidity was " + lowestFiles.get("Humidity") + " at " + lowestFiles.get("DateUTC"));

}

Aucun commentaire:

Enregistrer un commentaire