Hi guys I need help creating an unordered list of objects.I am supposed to create a file of name and phone number pairs, such that each name is on a separate line and each phone number is on a separate line. Then I am to edit the Phonetest and phone file to
-im supposed to create phone list and print
-then print the Ordered List of phone numbers.
Here is the class Phone:

public class Phone implements Comparable<Phone> {

  private String name;
  private String phone;

  public Phone(){
    name = "";
    phone = "";
  }
  public Phone(String name, String phone){
    this.name = name;
    this.phone = phone;
  }

  public String getName(){
    return name;
  }
  public String getPhone(){
    return phone;
  }
  public void setName(String name){
    this.name = name;
  }
  public void setPhone(String phone){
    this.phone = phone;
  }
  public String toString(){
    return (name + " " + phone);
  }
  public boolean equals(Phone other)
  {
      return (name == other.name)&&(phone == other.phone);

  }



@Override
public int compareTo(Phone other) {
    int compare = other.getPhone().compareTo(name);
    if (compare != 0)
        return compare;
    else 
    {
        if (other.getName().compareTo(name) < 0)
            return - 1;

        if (other.getName().compareTo(name) > 0)
            return 1;
    }

    return 0;   

    } 

}

Here is the class Phonetest:

public class PhoneTest {

   public static void main(String[] args) throws Exception {

   // get the filename from the user

      BufferedReader keyboard = new BufferedReader
                                 (new InputStreamReader(System.in),1);       
      System.out.println("Enter name of the input file: ");
      String fileName= keyboard.readLine();

   // create object that controls file reading and opens the file

      InStringFile reader = new InStringFile(fileName);
      System.out.println("\nReading from file " + fileName + "\n");

    // your code to create (empty) ordered list here
      ArrayOrderedList<Phone> list = new ArrayOrderedList<Phone>();   

   // read data from file two lines at a time (name and phone number)

      String name, phone;
      do {
        name = (reader.read());
        phone = (reader.read());

        // your code to add the entry to your ordered list here
        Phone phone2 = new Phone(name,phone);
        list.add(phone2);

      } while (!reader.endOfFile()); 

      System.out.println("Here is my phone book:");

      // your code to print the ordered list here

      System.out.println(list);



   // close file

      reader.close();
      System.out.println("\nFile " + fileName + " is closed.");      

   }
}

Here us my File:

So how can I change the print portion so I am getting the list in alphabitically order top down?
So I my file looks like this
Claire hfjg
555-222-1111
Bob abc
222-900-1818
Don cbcd
122-564-9653

I get this when I input my code:
Don cbcd 122-564-9653
Claire hfjg 555-222-1111
Bob abc 222-900-1818

How can I get the list in alhpabetical order so it looks more like this:
Bob abc 222-900-1818
Claire hfjg 555-222-1111
Don cbcd 122-564-9653

the compareTo should help you there.
but, just something I noticed:

public boolean equals(Phone other)
  {
      return (name == other.name)&&(phone == other.phone);
  }

This is bad code. What you need to do, is override the equals method of the Object class:

public boolean equals(Object o){
// code
}

a second thing I notice in your equals method, is that you are comparing the elements wrong.

the == operator will compare values correctly IF (and only if) we are talking about special types, like

  1. primitive types
  2. singletons
  3. enums

If, on the other hand, you want to compare values of objects, which is what you are doing, you need to use the equals method.

if you do use the == operator, you will be comparing references of the objects, not the values, which might (for Strings) give you the correct result, but which very well may not.

so, a (more) correct implementation would be:

public boolean equals(Object o){
    if ( ! (o instanceof Phone )){
      return false;
    }
    Phone actual = (Phone)o;
    return this.name.equals(actual.getName()) && this.phone.equals(actual.getPhone());
}

so, part of your trouble might very well be you were comparing your objects wrong. Can you implement your equals like this and try again?

This article has been dead for over six months. Start a new discussion instead.