简体   繁体   中英

trying to run public method from object in static methods in main, not working

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.

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 - eg 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.");
}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM