aspose file tools*
The moose likes Beginning Java and the fly likes How to store a name in an arraylist? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to store a name in an arraylist?" Watch "How to store a name in an arraylist?" New topic
Author

How to store a name in an arraylist?

Russel Crowe
Greenhorn

Joined: Jun 17, 2009
Posts: 2
Hai guys, iam learning java at the moment, and I got problem on arraylist.

Iam asked to do a store a name of a person, Ben, Luke, Hawk, and their age in to an arraylist and display it vertically, Suppose to be like this:

Name Age

Ben 15
Luke 14
Hawk 19

Here is what i done in main.java:
ArrayList<String> names = new ArrayList<String>();
names.add("Ben");
names.add("Luke");
names.add("Hawk");
System.out.println(names);

but it prints horizontaly, and the age will also too.
Can anybody have time to share the clue?

And 1 more think, we do this in main.java or in class.java ? iam waiting for reply^^

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're printing the entire list, not each name. In order to print the age you have to store it somewhere.
Russel Crowe
Greenhorn

Joined: Jun 17, 2009
Posts: 2
Hmm..so I should store all the 3 names in class.java and later call it in main.java right?

Can you give me a clue to do it? Thanks for your replay
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes, keeping the name and age in a separate class is probably a good idea--what have you tried so far?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store a name in an arraylist?