0

How would I compare an object that I created using scanner. To an object stored of the same class that I placed in the java link list.

This is where I get the object stored in the phone book class where my java link list is.

//this is the class I will use to 
 //create an object to store in the objects of link list
  //here I will store the records


   public class Person
{
    private String firstName;
     private String lastName;
    private String phoneNumber;

    public Person()
    {
    firstName="";
    lastName="";
    phoneNumber="";

    }
public Person(String nombre,String last, String number)
{
    firstName=nombre;
    lastName=last;
    phoneNumber=number;

    StringBuilder str =new StringBuilder(phoneNumber);
    str.insert(3,"-");
    str.insert(7,"-");
    phoneNumber=str.toString();


}

public Person(String nombre, String last)
{
    firstName=nombre;
    lastName=last;
    phoneNumber="";


}


}
//this will see if the two object are equal without comparing phone numbers
public boolean same(Person waco)
{
    boolean perro=false;

    if(firstName == waco.firstName && lastName == waco.lastName)
    {
        perro =true;

    }
        return perro;
}

This is how I add entry to my link List.

  //santiago is the link list


    if( input == 'n')
    {
        System.out.println("you have chose to add an entry");
        String first; //this will hold the firstName
        String last; //this will hold the last Name
        String phone; //this will hold the phoneNumber

        System.out.println("enter the first name");
        first=keyboard.nextLine();

        System.out.println("enter the last Name of this person");
        last=keyboard.nextLine();


        System.out.println("insert phone in the following form without dashses xxx-xxx-xxxx");
        phone = keyboard.nextLine();
        keyboard.nextLine();

        santiago.add(new Person(first,last,phone));
  }

Now what I am trying to do is see how a Person object I put into my list, is equal to a new Person item I created using scanner in this following part of the code

        if( input == 's')
        {

            System.out.println("enter a first Name");
            String first = keyboard.nextLine(); //put in here the first name
            System.out.println("enter a last name");

            String last  =keyboard.nextLine(); //put in the last Name

            Person agustin = new Person(first ,last);

             Person maria = santiago.get(0);

            System.out.println(agustin.same(maria));

     }

Now if santiago and maria objects have the same first and last Name the same method should return true

But my program returns to it to me as false. I am not sure why?

james Chol
  • 53
  • 2
  • 9
  • For Strings use equals() method not == operator – ema Mar 06 '15 at 22:47
  • what are you guys saying I should use the equals method? in my same method h.mmm. – james Chol Mar 06 '15 at 22:48
  • @james Chol one very nice option is String.compareTo(another String) gennerally the comparator methods can be used for many and difficult functions.== doesn't do the work and .equals() is one more option. – crAlexander Mar 06 '15 at 22:54
  • Correct if I'm wrong. You've created 2 different first-s and last-s string objects. So how they must be same? – ema Mar 06 '15 at 22:56

0 Answers0