0

I am building a generic tool in Java which reads data from a CSV and assigns it to some variables in the code and does bunch of stuff with it. I just realised that I should use an existing library (like SuperCSV) than doing it on my own. Now my question is the following: Since this is a generic tool, the datatypes of the values are not known. It depends what is present in the CSVfile. Now the user who would be building the CSV would know the datatypes. So I could add a "datatype" column in the CSV and the user could write "int" or "float" or "String" there. And then I could use if/then clauses in the code to assign the right datatype. But I am not sure if that is the most apt approach. Any thoughts? (I dont post on StackOverflow often so please advise if I am not being clear or detailed enough. Thanks!)

regards Anupam

3
  • 1
    I haven't used CSV-tools in java but I have used Jackson, which is a JSON library, and Jackson determines datatype automatically based on the fields and methods of a class. (In Jackson you read/write instance objects, therefore it can find out the fields and methods using reflection). The if-then approach sounds like a horrible way to do it, there should be other ways. Commented Aug 29, 2013 at 12:08
  • 1
    OpenCsv has a CSV/javabean adapter that probably does exactly that. Commented Aug 29, 2013 at 12:10
  • I would use the Integer and Double (or Float) classes to test if the item is integer, float or string. They have a static function valueOf() that will throw an exception if the string is not a valid format for that type. Commented Aug 29, 2013 at 12:37

1 Answer 1

1

This does not look too effective to pratically define data type before each data item in a CSV-like data processing. I would suggest to ask the CSV creator to specify the data types of each column beforehand in a predefined manner (say, specify a separate one-line CSV file with each data type, cause I think mixing raw data with metadata is not a good idea in this case). In my experience there was a similar task to process several types measurement tool data and raw data CSV files were as-is, metadata was specified in XML, but was not processed with each file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.