user2774325
user2774325

Reputation: 39

Java - Reading CSV and Converting Object[] values to String values

I am fairly new to Java and am having issue with the code below. The doAggregate method below is reading through the columns of data from a csv file. I want to be able to check the value of fields[i] for special characters and non-escaped quotes and remove them from the value before they are appended. The code as is errors at the point noted below: java.lang.Double cannot be cast to java.lang.String. So it sounds like not all the types of fields will convert to a String value. Is there a good way to test if the String is a String? Or is there a better way of going about this?

public String doAggregate(Object[] fields) {
    if (ObjectUtils.isEmpty(fields)) {
        return "";
    }
    if (fields.length == 1) {
        return "\"" + ObjectUtils.nullSafeToString(fields[0]) + "\"";
    }
    String tmp_field_value;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < fields.length; i++) {
        if (i > 0) {
            sb.append(getDelimiter());
        }
        sb.append("\"");
        //start new code
        tmp_field_value = (String) fields[i];
        //error on line below
        sb.append(getCleanUTF8String(tmp_field_value));
        //end new code
        //start old code
        //sb.append(fields[i]);
        //end old code
        sb.append("\"");
    }
    return sb.toString();
}

public String getCleanUTF8String(String dirtyString){
    String cleanString = "";
    try {

        byte[] cleanBytes = dirtyString.getBytes("UTF-8");
        cleanString = new String(cleanBytes, "UTF-8");
        cleanString = cleanString.replace("\"", "\\\"");
        cleanString = cleanString.replace("'", "\\'");
    } catch (UnsupportedEncodingException uee){
        System.out.println("*******ERROR********: Unable to remove non UTF-8 characters in string: |" + dirtyString + "| -- Java Error Message:" + uee.getMessage());
        //TODO - may need to revisit this next line, some additional character checks may need to take place if the character set exclusion fails. 
        cleanString = dirtyString;
    }

    return cleanString;
}

Upvotes: 1

Views: 334

Answers (1)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

instead of doing tmp_field_value = (String) fields[i] do like below code.

 if(fields[i]!=null){ 
     tmp_field_value =  fields[i].toString();
 }

Upvotes: 3

Related Questions