views:

62

answers:

2

Hi,

My problem is the comparision of two objects and the strings that they return (accessed through getters).

Object one parses a csv file for dates and when printed out through exampleObject.getDateTime() returns the string: "2010-03-26-10-54-06.471000"

Object two has its dateTime string set by the user. When I set the dateTime on object two to be the same as objectOne and then do exampleObjectTwo.getDateTime() it returns 2010-03-26-10-54-06.471000

So the main difference is that one string has quotations from parsing the csv (which contains no quotations) and the user set string when returned has no quotations!

If anyone can offer an explanation as to why this is happening I'd be very grateful!

Many thanks!


        BufferedReader input = new BufferedReader(new FileReader(file));

        try {
            String line = null; 
            while ((line = input.readLine()) != null) {
                SearchResult searchResult = new SearchResult();

                if (!line.contains("Date")) {
                    String[] split = line.split(",");
                    SearchResult.setDateTime(split[0]);
                    SearchResults.add(SearchResult);
                }

            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

edit above is the code that was using to parse the csv file. I checked and the csv file does not contain any quotations.

Thanks for the quick and helpful response!

A: 

check again how do you perform csv parsing.

or remove quotations from string:

String newDate  = oldString.replaceAll("\""," ").trim();
Wajdy Essam
This removes **all** doublequotes. This is a bit drastic as general solution. A CSV field can in turn also contain legitimate doublequotes.
BalusC
he should use this line after call: String oldString = exampleObject.getDateTime() ; and all the return values from getDateTime doesn't contain doublequotes inside it.
Wajdy Essam
+1  A: 

You need to modify/configure the CSV parser to remove the quotes.

If it's a homegrown CSV parser, doing so should suffice to get rid of the surrounding doublequotes:

field = field.replaceAll("^\"|\"$", "");

If it's a 3rd party API, then you need to consult its documentation (or mention the library name here so that one who is willing to do that can lookup the documentation for you).

See also:

BalusC
Thanks for the response, please see code above in the edit.
avengedsixfold
Splitting on the comma is also a bad idea. There might be quoted fields which in turn contains a comma, e.g. `"field1","fie,ld2","fie""ld3"` is a legitimately valid CSV row which should be parsed as `field1`, `fie,ld2` and `fie"ld3`. Once again, check the link I provided for a good parser example.
BalusC
Ooops had missed that link!! Thanks again
avengedsixfold