aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList with JavaBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList with JavaBean" Watch "ArrayList with JavaBean" New topic
Author

ArrayList with JavaBean

Booma Devi
Ranch Hand

Joined: Nov 02, 2011
Posts: 61
Hi,

I am new to Java.. I need help please..

I have one CSV file like the below (without heading)

AAA,123
BBB,125

I need to read the CSV file and mapping to java bean.. I wrote the code like this.. But it doesn't work...


javaBean:

Main Class:




I want to return the list of elements (Name, Age)....
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hello Booma Devi,

Please post complete code. I'm assuming that class CsvReader works properly.

Also, what is 'products' at line 15 in PersonDao class? (you are calling it as Main class, but I do not see any main method).

Next to this, you are returning a List of Person objects, and each Person contains a name and age (why age is String by the way?), and if I understand correctly, you want to return simply name and age pairs instead of List of Person object.

In this case, you can use a Map (say HashMap), but again, you'll lose duplication with it. e.g. if you are having name as key then you cannot put two persons with same name but different age in HashMap (which is possible in List).

So, I would prefer what you are doing - returning the List itself.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Booma Devi
Ranch Hand

Joined: Nov 02, 2011
Posts: 61
Hi Anayonkar,

Thank you so much for your quick response... My requirement is I need to send the list of Person objects to the client side (Flex)...


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You have to create a new Person object for every line you read. Move the object creation code inside your while loop, so that for each line a new object is created and added into the List.
Rajat Jindal
Ranch Hand

Joined: Sep 19, 2011
Posts: 42
Booma Devi wrote:Hi Anayonkar,

Thank you so much for your quick response... My requirement is I need to send the list of Person objects to the client side (Flex)...





You should also return list after try catch block.. suppose while working you get some exception that will be handled in catch block but tell me in that case you will not be able to return the type list, which is wrong as method return type is List<Person> .
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Rajat's correct - I missed to see the stated error in the code block.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

You are welcome.
Booma Devi wrote:I got some error here like "This method must return a result of type List<Person>"

This is because your return statement is in try block. Outside the block, there's no return statement.
I would suggest to put only relevant code inside try block and everything else should be outside try block. Also, do not catch Exception directly (rather catch some meaningful exception like FileNotFoundException etc.) e.g.
declare list
try {
read file
populate file data
}
catch (file operation related exception) {
print stack trace etc.
return null - because there's no point in iterating over file data now
}
iterate over the file data and populate list
return list


Secondly, in your main method, you are calling getList two times, and during first invocation, you are not storing the returned value in any variable. Please try to avoid this - you are doing all file operations two times unnecessarily.

I hope this helps.
Booma Devi
Ranch Hand

Joined: Nov 02, 2011
Posts: 61

Thanks Anayonkar... I will try and update you.. Thanks for your tips..
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Sure. You are welcome.
Booma Devi
Ranch Hand

Joined: Nov 02, 2011
Posts: 61

Hi Anayonkar, Now my code is working fine... Thank you so much for spent the time to read my code..
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Welcome. I'm glad that you are able to fix your code
Booma Devi
Ranch Hand

Joined: Nov 02, 2011
Posts: 61
Thanks to John and Rajat... :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList with JavaBean