Reputation:
I am making a program that has an array of people. The people have different amounts of info for instance they all have a first name, last name, type, gender. The first issue I have is some also have email and some have a image (using Icon) and some have both of them.
I need to read these pieces of data in and then display them, it is for a college class and the teacher was basically like figure it out.. I have read the API and numerous articles and can't seem to get it to work. Can someone give me a push in the right direction?
I am not looking for you to hand me the answers just a little help.
Upvotes: 0
Views: 82
Reputation: 5837
Read the file line by line and split line with ,
.
// you need to create a pojo to hold all the user info.
List<UserObject> users = new ArrayList<UserObject>();
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] userinfos = line.split(",");
UserObject newUser = new UserObject();
//set the mandatory attributes here
if (userinfos.length > 4) {
// you have the extra fields here.
// set the extra fields to user here
}
users.add(newUser);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
One problem with this is first name or last name might have commas with in them. I suggest you to use any third party csv parser like Open Csv.
Upvotes: 1