|
-
October 24th, 2011, 10:43 PM
#1
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
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
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));
}
}
}
Please help!
-
October 25th, 2011, 03:06 AM
#2
Re: PhoneBook entries with ArrayList problem
You are probably getting a compile error saying that a symbol cannot be found, adding that it is the method add(java.lang.String,java.lang.String).
Read the API for the List interface (or the ArrayList class) and you will see that one add method (the one I think you are trying to use) expects one object, not two: you should be calling add with a PhoneBook object, not two Strings.
-
October 25th, 2011, 04:43 AM
#3
Re: PhoneBook entries with ArrayList problem
You have a list of PhoneBook objects, to which you attach two Strings. Add proper elements to your list and you will get forward. Creating a single instance of PhoneBook does absolutely nothing in terms of adding anything to the list. You seem to have some clear confusion of a lot of basic things.
-
October 25th, 2011, 06:12 AM
#4
Re: PhoneBook entries with ArrayList problem
Try
Code:
entries.add( new Phonebook("Greg", "478-454-7605") );
instead of
Code:
entries.add("Greg","478-454-7605");
Explanation: entries is a list of objects of type Phonebook - so you can't add 2 strings. You need to construct a new Phonebook object from the name and number, then add this to the list.
-
October 25th, 2011, 11:35 AM
#5
Re: PhoneBook entries with ArrayList problem
Thank you that did work but everytime i try and print the results in JOPtionPane i get this as a result "Program_4.PhoneBook@863399"
I dont know what this means
-
October 25th, 2011, 05:01 PM
#6
Re: PhoneBook entries with ArrayList problem
When you call the showMessageDialog:
Code:
JOptionPane.showMessageDialog(null, entries.get(x));
entries.get(x) is converted to a string by calling the toString method of the object. You do not override this method in your PhoneBook class, so the default is called (i.e. the Object class toString). Just add a toString method to your class to make it display whatever you want.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|