1

Ok, so I have this mini program I'm creating called Mobile Phone. It has 3 classes: Main.java, Contact.java and MobilePhone.java.

Main.java executes static methods with use of scanner to show and return information through what would be a phone menu. Contact.java is simple an class that stores a name and an number as a contact. MobilePhone.java has as ArrayList which stores the objects within the Contact class as contacts and then has several methods like addContact(); removeContact(); queryContact(); modifyContact(); etc...

The problem I'm having is that I am running these static methods in Main.java and some of them call upon the public non static methods in MobilePhone.java and they don't seem to be executing all the code within the methods. Here's an example:

In Main.java I have a method called addContact(); which looks like this: note mobilePhone (case sensitive) is the name of the new MobilePhone class.

private static void addContact(){
    System.out.println("Enter name: ");
    String name = scanner.nextLine();
    System.out.println("Enter number: ");
    String number = scanner.nextLine();
    Contact newContact = Contact.createContact(name, number);
    mobilePhone.addContact(newContact);
}

mobilePhone.addContact(); looks like this:

public void addContact(Contact contact){
    for(int i = 0; i < this.contacts.size(); i++){
        if(this.contacts.get(i).equals(contact)){
            System.out.println("Contact already exists.");
        } else {
            this.contacts.add(contact);
            System.out.println("New contact: " + contact.getName() + " - "
                    + contact.getNumber() + " added.");
        }
    }
}

What's happening is that scanner is taking the inputs from Main.addContact(); but when it gets to the line mobilePhone.addContact(newContact); it doesn't seem to execute anything within that function. It seems that way because nothing is being printed from either System.out.println() in any case.

I have another function with prints all the objects within the ArrayList called printContacts(); and it too isn't printing:

public void printContacts() {
    System.out.println("Contacts List: ");
    for(int i = 0; i < contacts.size(); i++){
        System.out.println("#" + (i+1) +". " + this.contacts.get(i).getName()
                + " - " + this.contacts.get(i).getNumber());
    }
}

I can't figure out what the problem is. I'm not getting an error, I'm just not getting the return information that I want.

mphilpot22
  • 13
  • 2

2 Answers2

4

In MobilePhone.addContact(), you have the code for adding the contact inside the for loop - so it adds the contact every time another contact already present is not equal.

At start, there are not contacts (I assume), so it adds the contact...never. So there's no way to add any contacts. All other problems you observe are likely consequence of this - e.g. there won't ever be anything to print if you cannot add anything.

Fix:

public void addContact(Contact contact){
    for(int i = 0; i < this.contacts.size(); i++){
        if(this.contacts.get(i).equals(contact)){
            System.out.println("Contact already exists.");
            return;
        }
    }

    this.contacts.add(contact);
    System.out.println("New contact: " + contact.getName() + " - "
        + contact.getNumber() + " added.");
}
Jiri Tousek
  • 11,783
  • 5
  • 25
  • 41
  • Ok, so I used the code listed above but it still doesn't seem to execute what's going on inside the for loop. If I add a new contact it executes what's outside of the for loop, but if I add the same contact again it is not giving me the System.out.println("Contact already exists."); – mphilpot22 Feb 06 '17 at 21:00
  • Did you debug it? It might be wrong implementation of `equals()` in `Contact` or whatever. – Jiri Tousek Feb 06 '17 at 21:02
  • I'm not quite sure how to use the debugger in Intellij. I'm still very new to this. I'm trying to understand it but it's not giving me much information. I can find the debugger option but there are two windows frames and threads and all threads says is: "Attach Listener"@600: RUNNING "Finalizer"@602: WAIT "main"@1 in group "main": RUNNING "Reference Handler"@603: WAIT "Signal Dispatcher"@601: RUNNING. Also, sorry I don't know how to format lines in comments to make that easier to read. – mphilpot22 Feb 06 '17 at 21:07
  • I'm going through it now, might have figured out how to use it. I'll let you know if I find anything out. – mphilpot22 Feb 06 '17 at 21:16
  • I couldn't get the debugger figured out but I went back to the .equals() thing. Neither .equals nor == give me an error but if I change the code in the if statement to: if(this.contacts.get(i).getName().equals(contact.getName())) { Not sure why I can't compare objects, but for now I guess I'll just compare names. It seems to work that way anyways. Sorry, I still can't figure out how to get code formatting in comments. – mphilpot22 Feb 06 '17 at 21:40
1

You cannot use equals() method to compare two objects unless you override it to do so. By default the equals() method of two objects only returns true if it is the same instance. (String would be an exception here.)

In your case, since the potential new contact object you might add into the list, is not the same as any existing one, it fails.

There are two possible fixes for this:

1.     if(this.contacts.get(i).getName().equals(contact.getName())) 

is one way to check if it is indeed the same contact you are adding.

  1. You can override the equals() method with a custom implementation on your Contact object.

    Contact {           
    @Override
     public boolean equals(Object obj) {
     if (obj == null) {
       return false;
     }    
    if(this.name.equals(obj.getName()))
      return true;
    else
      return false;
       }
     }
    

You could even add a check to both by name and phone number or whatever you wish to. Note the implications of having a custom equals() method on hashCode ().

Please refer this link for more information What issues should be considered when overriding equals and hashCode in Java?

Community
  • 1
  • 1