mphilpot22 mphilpot22 - 4 years ago 108
Java Question

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.

Answer Source

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.");
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download