ArrayList manipulation and Comparators

Hi! I posted here yesterday (http://forum.java.sun.com/thread.jspa?threadID=5167975) and have since made some progress with my problem and encountered some new ones.

I'm really really new to this, and I'm sorry if it's frustrating to see newbies asking question after question. I'm reading in data about people from a .txt file and turning it into Strings and Integers:

publicstaticvoid main (String[] args)

{

ArrayList<Person> personList =new ArrayList<Person>();

People peopleDetails =new People();

people =new File("people.txt");

try{

input =new BufferedReader(new FileReader(people) );

while (( inputLine = input.readLine()) !=null){

StringTokenizer st =new StringTokenizer(inputLine);

//This will only work provided that each line only has 4 tokens

inputFirstName= st.nextToken();

inputSurname= st.nextToken();

inputDay = Integer.parseInt(st.nextToken());;

inputMonth= Integer.parseInt(st.nextToken());

// Creating a new person

newPerson =new Person(inputFirstName, inputSurname, inputDay, inputMonth);

System.out.println(inputFirstName +" " + inputSurname +"'s birthday is on the " + inputDay +

" day of the " + inputMonth +" month.");

System.out.println("*************");

System.out.println(" ");

// Adding the person to the ArrayList of people

personList.add(newPerson);

}

}

catch (FileNotFoundException ex){

System.out.println("File not found");

}

catch (IOException ex){

System.out.println("IO exception");

}

The output looks like this:

Michael Bluth's birthday is on the 14 day of the 6 month

****************

Lucille Bluth's birthday is on the 31 day of the 3 month

****************

etc, which is fine for now because I know I'm at least processing the file. When I try to print the contents of the ArrayList using

System.out.println(personList);

all I get is "[Person@51127a, Person@94257f, Person@29c58e, Person@691dee, Person@12bf892, Person@1cc5069, Person@b32ed4, Person@1c7980c, Person@cf710e]". Can anybody help me figure out why?

I'm also having no luck whatsoever sorting and printing the list of people in order of their birthdays, is there anybody that can help? Thanks so much.

[3484 byte] By [KatieMa] at [2007-11-27 3:14:19]
# 1

The Person class does not have a meaningful toString() method.

You should override the toString() method to provide meaningful data associated with the object.

To have data in a certain order requires you to sort it in some manner or another. I'm off to another boring meeting but I'm sure one of my esteemed colleagues will continue for me.

Good luck

filestreama at 2007-7-12 8:16:50 > top of Java-index,Java Essentials,New To Java...
# 2
Have fun in your meeting, streamy! :pillow: @OP: The collections offer various ways of sorting. Did you read this tutorial yet? http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
PhHeina at 2007-7-12 8:16:50 > top of Java-index,Java Essentials,New To Java...
# 3

> Have fun in your meeting, streamy! :pillow:

>

>

> @OP:

> The collections offer various ways of sorting. Did

> you read this tutorial yet?

> http://java.sun.com/docs/books/tutorial/collections/in

> terfaces/order.html

Yep, but I'm rereading it now. It might not look like it, but I've been working on this for hours and my brain's getting a bit fried. The paragraph under the heading "Object Ordering" says that you can only compare like for like. I think this means I can't just use the Collections.sort(x) method, because my ArrayList has both strings and integers stored in it. Is that right? So I'm going to have to write my own Comparable type, and is that where the toString() method mentioned by the previous poster comes in?

Why is it important to use toString if the values I'm interested in sorting are the integers representing the day and month someone was born?

KatieMa at 2007-7-12 8:16:50 > top of Java-index,Java Essentials,New To Java...
# 4

I'm back from my meeting. I didn't have the opportunity to take a decent nap!

You should override toString() to provide meaningful information about the state of the object. Otherwise you'll use java.lang.Object's toString() which provides the name of the class plus the hashcode which provides little to no value in debugging. Imagine how much more useful the following is, during development.

public class Person

{

private String surname;

private String givenName;

private double leftAntecubitalFossaWidth;

...

/**

* override Object's toString()

*/

public String toString()

{ return new String(surname+", "+givenName+", "+leftAntecubitalFossaWidth); }

}

filestreama at 2007-7-12 8:16:50 > top of Java-index,Java Essentials,New To Java...
# 5

> I'm back from my meeting. I didn't have the

> opportunity to take a decent nap!

> [/code]

Don't worry, I'm sure my questions will send you right off to sleep. I've got toString to return "Rupert Murdoch", the last name, as a String but I'm unsure as to how I can get toString to work for each loop. When I ask my program to print my ArrayList afterwards, "null" is the response.

KatieMa at 2007-7-12 8:16:50 > top of Java-index,Java Essentials,New To Java...
# 6

In your looping structure, you obtain a Person element from the array/array list/vector/list/whatever and call the toString() method.

If the result of your toString() is a NullPointerException or if the element obtained from your "container" is null, then you have not populated the container correctly.

filestreama at 2007-7-12 8:16:50 > top of Java-index,Java Essentials,New To Java...