views:

45

answers:

2

Hey everyone!

I'm working with Super CSV and it looks like an amazing package.

My only worry is how to work with columns with spaces in their names. No, I cannot go back and remove the spaces myself. These files will be given to me by the hundreds and I don't have time to go back and fix all 60 columns for each file and I can't trust everyone else to do it properly.

How can I work with columns with spaces in the title (i.e. "First Name" not "FirstName" or "firstName")?

Thanks!

For coding samples, look here: http://supercsv.sourceforge.net/codeExamples_general.html

+1  A: 

You notice this line in the samples you link to:

final String[] header = inFile.getCSVHeader(true);

This should give you your column names, no?

http://supercsv.sourceforge.net/javadoc/index.html

I think I understand your question now. The String[] argument passed to the read function takes the property names of the class you want to read into. It is positional, so it doesn't have to be named anything like the headers. So, for example, you can have String[] header = inFile.getCSVHeader(), but then have a mapping of headerName->propertyName, so, if your header fields were:

First Name, Last Name, Age

but your class was

getFirstName(), setFirstName(...);
getLastName(), setLastName(...);
getYears(), setYears();

pass to the read method not (String[]) {"First Name", "Last Name", "Age"}, as your header is, but pass to read, a (String[]) {"FirstName", "LastName", "Years"} array.

maxwellb
Look at the UserBean class, though. Doesn't the column name have to correspond to the variables? I'm a little confused what happens at the while loop.
Justian Meyer
What I'm saying is, notice how he uses the headers "username", "password", etc. These directly correspond to the variables username and password. I have "First Name" as a header and can't have First Name as a variable (spaces aren't allowed + poor formatting).
Justian Meyer
updated now that i understand
maxwellb
I was thinking about that, but if I define my own header, wouldn't it consider my headers that are already in place as data? I'll try it out and get back to you. Might take a sec with so many variables.
Justian Meyer
No, according to the JavaDoc, everything in the `read` methods only correspond the position of the String[] to the position of the field. This is easily seen if you use a CsvListReader instead of a CsvBeanReader, as there are no name mappings of field names to speak of. You only care about position with CsvListReader. With CsvBeanReader, String[] {FName,LName} says "read the first field into the FName property, and the second field into the LName property". After the header, Csv*Reader doesn't care what you name the fields. It's 1st field, 2nd field, 3rd field, etc.
maxwellb
It all works perfectly now. Thanks :) EDIT: It does include the headers like I expected it to, but those can easily be worked around.
Justian Meyer
+1  A: 

If you want to map CSV heade with different name, you can create a hasmap and use this for your internal implementation, e.g. you can map "First Name" with "firstName" and populate your bean based on your internal names..

That sounds like what I'm looking for. Can you help me through it a bit? I'm really unfamiliar with this. Thanks! :)
Justian Meyer
I hope you got the solution as suggested by maxwellb..