I have an assignment, where I need to read a CSV file. I know that the first line of the CSV file will have the following information:
LabelA,LabelB,LabelC,LabelD
and that all of the lines below will have the corresponding information, so all the following lines of the CSV file looks like this:
InfoA,InfoB,InfoC,InfoD
I have created a class that stores all of that information, and then I need to create a HashMap to store all of those objects, each object with the information of a different line, however, the CSV File that I will be reading has a randomized order of the columns, so I might receive a CSV File that has the first line as:
LabelD,LabelA,LabelB,LabelC
Which would make the following lines as:
InfoD,InfoA,InfoB,InfoC
How can I approach this? Regardless of the order of the columns of the CSV file, I need to correctly store that information in an object.
Before this assignment, the order of the columns was always the same, so I was storing each line on a string, then splitting the string, and then putting the information in that order in the constructor of the object, but I can no longer do this, since the order will be randomized.
I am not allowed to use any dependency other than java.io.File, java.util.Scanner, and java.util.HashMap.
If you want to use core java only you can try this: