1

How can I validate that the data of CSV file contains headers? I am using supercsv and Java. My csv file contains;

ACCOUNT,NAME,PHONE (header) <--- validation needed, all headers must present Checking,John,1122222222 (data)

Saving,Carl,1234567891 (data)

Thank you for your help.

1
  • Welcome to StackOverflow :) As @fge says, you really should document what you've tried. It makes it easier for others to answer, and you're more likely to get an upvote on your question. Commented Jan 21, 2013 at 2:15

1 Answer 1

7

If you don't know what the header values are beforehand, there's no way to know if the first line is a header or not (it just looks like another data line).

If you do know what the header values should be, then you could simply make sure that all the expected columns are there (it doesn't matter what order, as long as they're there).

For example

List<String> expectedHeaders = Arrays.asList("ACCOUNT","NAME","PHONE");

String[] header = csvReader.getHeader(true);

if (!Arrays.asList(header).containsAll(expectedHeaders)){
    // not all headers present - handle appropriately 
    // (e.g. throw exception)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Hound Dog for your suggestion.
No problem @Dharanae, don't forget to select this as the correct answer (if it is!)

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.