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 4 of 4

Thread: PhoneBook entries with ArrayList problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PhoneBook entries with ArrayList problem

    Hi I am supposed to create a class and create a constructor and accessors and mutators. then i must create a program that crates 5 instance objects of the PhoneBook class and stores them in an ArrayList. then use a for loop to display all 4 instances in the Array List.

    The Problem i am having is actually hard coding the name and numbers in there and getting them to be displayed.

    Here is my code for Phone Book class

    HTML Code:
    package Program_4;
    
    public class PhoneBook {
    
    	private String name;
    	private String number;
    	  public PhoneBook() { 
    		  name = null;       
    		  name = null;    
    		  }
    
    	
    public PhoneBook(String phonename, String phonenumber){
    	name=phonename;
    	number=phonenumber;
    }
    public void setname (String phonename){
    	name=phonename;
    
    }
    public String getname(){
    	return name;
    
    }
    public  void setnumber (String phonenumber){
    	number=phonenumber;
    }
    public String getnumber(){
    	return number;
    }
    
    
    }
    Here is my program

    HTML Code:
    package Program_4;
    import java.util.ArrayList;
    
    import javax.swing.JOptionPane;
    public class PhoneBookDemo {
    
    	
    	public static void main(String[] args) {
    	PhoneBook book=new PhoneBook();
    	ArrayList <PhoneBook> entries=new ArrayList<PhoneBook>();
    	entries.add("Greg","478-454-7605");
    	entries.add("Peter","478-448-7975");
    	entries.add("Bob","478-234-7650");
    	entries.add("John","478-423-7905");
    	entries.add("Jim","478-124-7235");
    	
    	
    	for (int x = 0; x < entries.size(); x++){
    	
    	JOptionPane.showMessageDialog(null, entries.get(x));
    	}
    	
    	
    	
    	}
    
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: PhoneBook entries with ArrayList problem

    entries.add("Greg","478-454-7605");
    You need to create an instance of the PhoneBook class and pass that to the add method, not two Strings.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PhoneBook entries with ArrayList problem

    Thank you for your quick response. Where do i exactly create this instance in my code?

    thanks

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PhoneBook entries with ArrayList problem

    Hi,
    The modified code will as follow:
    for adding the object in the list,

    entries.add(new PhoneBook("Jim","478-124-7235"));

    bean factory
    Last edited by abani; December 18th, 2011 at 01:00 AM.

Similar Threads

  1. Java PhoneBook
    By Sparky in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 6th, 2011, 03:13 AM
  2. Problem with ArrayList
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2010, 08:37 PM
  3. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM
  4. HELP!!! Entry Class to represent entries in a telephone directory
    By Princess D in forum Java Theory & Questions
    Replies: 10
    Last Post: January 22nd, 2010, 04:39 AM
  5. Create Phonebook using inheritance
    By islandGirl in forum Object Oriented Programming
    Replies: 5
    Last Post: February 17th, 2009, 03:42 AM