0

I am trying to add some names and phone numbers created from the constructor of the PhoneBookEntry class, store them inside of an ArrayList inside of the PhoneBook class, and then be able to print out the array list.

public class Application {
  public static void main(String[] args) {
    PhoneBookEntry name1 = new PhoneBookEntry("Cameron", "1-425-415-7157");
    PhoneBookEntry name2 = new PhoneBookEntry("Mike", "1-748-142-2341");
    PhoneBookEntry name3 = new PhoneBookEntry("Riles", "1-471-648-1782");
    PhoneBookEntry name4 = new PhoneBookEntry("Tom", "1-427-145-6471");
    PhoneBookEntry name5 = new PhoneBookEntry("Billy", "1-718-545-5715");
  }
}


import java.util.ArrayList;

public class PhoneBookEntry {

PhoneBook book = new PhoneBook();

public PhoneBookEntry(String name, String phoneNumber) {
    book.add(name, phoneNumber);
}

public void printEntries() {
    for(int i = 0; i < names.size(); i++) {
        System.out.println("Name #"+(i + 1)+": "+names.get(i));
    }
}

}


import java.util.ArrayList;

public class PhoneBook {
   ArrayList<String> names = new ArrayList<String>();
  ArrayList<String> phoneNumbers = new ArrayList<String>();

public void add(String name, String phoneNumber) {
    names.add(name);
    System.out.println(name + " added to the arraylist!");
    System.out.println(names.size());
    phoneNumbers.add(phoneNumber);
}

public void print() {
    for (int i = 0; i < names.size(); i++) {
        System.out.println("Name #" + (i + 1) + ": " + names.get(i));
    }
    for (int i = 0; i < phoneNumbers.size(); i++) {
        System.out.println("Phone Number #" + (i + 1) + ": " + phoneNumbers.get(i));
    }
}
}

Current output:

Cameron added to the arraylist!
1
Mike added to the arraylist!
1
Riles added to the arraylist!
1
Tom added to the arraylist!
1
Billy added to the arraylist!
1
5
  • 1
    Where is your book ArrayList declared? Commented Apr 7, 2016 at 0:21
  • Secondarily, it'd be MUCH easier just to use a centralised Map (probably TreeMap). Utilise parametrisation with <String, String> unless you feel it would be easier to construct a new wrapper for phone numbers (probably called PhoneNumber :P) Commented Apr 7, 2016 at 0:22
  • I updated the post, sorry Commented Apr 7, 2016 at 0:23
  • @ifly6, This is for a class, and in the directions, you have to use an arraylist for this. Commented Apr 7, 2016 at 0:25
  • Can you add any system.outs for phone information as well. I see you have added it for names Commented Apr 7, 2016 at 0:27

3 Answers 3

3

It's because you create a new PhoneBook for each PhoneBookEntry, ending up with five lists with one entry each.

Your code should probably look more like

public static void main(String[] args) {
   PhoneBook book = new PhoneBook();
   book.add("Cameron", "1-425-415-7157");
   book.add("Mike", "1-748-142-2341");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I totally forgot about that.
1

You are creating a new PhoneBook with every PhoneBookEntry.

Make PhoneBook booka field:

public PhoneBook book = new PhoneBook();

and simply add each new entry into it with every new PhoneBookEntry object. Or simply create your PhoneBook book in the main.

Comments

0

Why not create one phonebook, Have the array list of phone book entries in there, and then just add entries to the phonebook?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.