Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Phone Book Program

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Phone Book Program

    I can't figure out what I did wrong here. I feel as though it's very obvious, but I can't figure it out for the life of me right now. Any help would be greatly appreciated.

    /**
    The class, PhoneBookEntry, has fields
    for a person's name and number.
    */
     
    public class PhoneBookEntry
    {
    	private String names;
    	private String phoneNumber;
     
    	public PhoneBookEntry(String name, String number)
    	{
    		names = name;
    		phoneNumber = number;
    	}
     
    	public void setName(String name)
    	{
    		names = name;
    	}
     
    	public String getName()
    	{
    		return names;
    	}
     
    	public void setNumber(String number)
    	{
    		phoneNumber = number;
    	}
     
    	public String getNumber()
    	{
    		return phoneNumber;
    	}
    }

    /**
    The program, PhoneBook, creates 5
    PhoneBookEntry objects and stores them
    in an ArrayList. A loop then displays
    the contents in the ArrayList.
    */
     
    import java.util.ArrayList;
     
    public class PhoneBook
    {
    	public static void main(String[] args)
    	{
    		ArrayList<PhoneBookEntry> list = new ArrayList<PhoneBookEntry>();
     
    		list.setName(new PhoneBookEntry("Steve"));
    		list.setNumber(new PhoneBookEntry("(908)651-9685"));
    		list.setName(new PhoneBookEntry("Rachel"));
    		list.setNumber(new PhoneBookEntry("(732)855-4658"));
    		list.setName(new PhoneBookEntry("John"));
    		list.setNumber(new PhoneBookEntry("(732)743-8102"));
    		list.setName(new PhoneBookEntry("Tommy"));
    		list.setNumber(new PhoneBookEntry("(732)421-0332"));
    		list.setName(new PhoneBookEntry("Amanda"));
    		list.setNumber(new PhoneBookEntry("(732)947-5245"));
     
    		for(PhoneBookEntry object : list)
    		{
    			System.out.println("Name: " + "		" + "Number: ");
    			System.out.println(list.getName() + "		" + list.getNumber());
    		}
    	}
    }

    Thanks in advance.


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Phone Book Program

    good day


    try this, also how do i insert this the way you inserted your code? sorry, im new to this
    public static void main(String args[]) {

    ArrayList<PhoneBookEntry> list = new ArrayList<PhoneBookEntry>();

    list.add(new PhoneBookEntry("Steve", "(908)651-9685"));
    list.add(new PhoneBookEntry("Rachel", "(732)855-4658"));
    list.add(new PhoneBookEntry("John", "(732)743-8102"));

    System.out.println("Name: " + " " + "Number: ");
    for(PhoneBookEntry object : list)
    {

    System.out.println(object.getName() + " " + object.getNumber());
    }



    }

    regards

  3. The Following User Says Thank You to destitute.developer For This Useful Post:

    trubble (February 7th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Phone Book Program

    Worked! Thanks alot!

    Also, to get the code inserted/highlighted you have to type: [/code] at the end of your code, and [code=Java] at the beginning of it

  5. The Following User Says Thank You to trubble For This Useful Post:

    destitute.developer (February 7th, 2014)

  6. #4
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Phone Book Program

    nice i'll remember that. thanks.

  7. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Phone Book Program

    @destitute.developer: Welcome! Please read this topic to learn how to post code correctly and other useful info for new members.

Similar Threads

  1. Phone book program, where to begin?
    By smithmar in forum Java Theory & Questions
    Replies: 1
    Last Post: January 22nd, 2014, 12:29 AM
  2. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  3. phone book code problems
    By mu'min in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 16th, 2010, 11:36 AM
  4. phone book code problems
    By mu'min in forum Member Introductions
    Replies: 1
    Last Post: December 16th, 2010, 09:07 AM
  5. phone book
    By Skat in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 3rd, 2010, 09:56 AM