Results 1 to 20 of 21
Thread: Phone book arrayList problem
- 11-09-2011, 08:18 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Phone book arrayList problem
In this program im trying to write a program that creates at least five instance objects of the PhoneBook and stores
them in an ArrayList. this is what i have so far but i keep getting the error of PhoneBook cannot be resolved to a type? so im having problems adding the entries to my array list. ive been working on this for hours and aam stuck any help would be much appreciated thank you
here is my other one:Java Code:package PhoneBookProgram; import java.util.ArrayList; import javax.swing.JOptionPane; public class PhoneBook { public static void main(String[] args) { ArrayList <PhoneBook> entries=new ArrayList<PhoneBook>(); entries.add( new PhoneBook("Robert","555-555-555"); entries.add( new PhoneBook("Jim","478-124-7235")); entries.add( new Phonebook("Greg", "478-454-7605") ); entries.add( new Phonebook("Greg", "478-454-7605") ); entries.add( new Phonebook("Greg", "478-454-7605") ); for (int x = 0; x < entries.size(); x++){ } } }
Java Code:package PhoneBookProgram; public class PhoneBookDemo { private String name; private String number; 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; } }
i also need to return the arrayList with a "for" method and im having problems debugging it so it wont run. thanks again
- 11-09-2011, 08:19 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: Phone book arrayList problem
and yes i know they are the same inputs i just was trying to get it to work before i actually put the right names and numbers thank you
- 11-09-2011, 08:43 PM #3
Re: Phone book arrayList problem
You need to have a class named PhoneBook with a constructor with the args that you are calling it with.i keep getting the error of PhoneBook cannot be resolved to a type
It looks like you have either misnamed the classes or put the constructor you need in the wrong class.
- 11-09-2011, 09:20 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: Phone book arrayList problem
i do have a class PhoneBook and i added constructor: PhoneBook myPhoneBook= new PhoneBook(); but still is giving same error. maybe im not understanding what your saying, can you please explain? thanks for quick responce
- 11-09-2011, 09:22 PM #5
Re: Phone book arrayList problem
PhoneBook myPhoneBook = new PhoneBook(); is a call to a constructor with no args.
You need to define a constructor with in the PhoneBook class that matches how you are calling it:
new PhoneBook("Robert","555-555-555");
Here it has two Strings.
- 11-09-2011, 09:24 PM #6
Re: Phone book arrayList problem
It looks like you have either misnamed the classes or put the constructor you need in the wrong class.
Try swapping the names of the classes that you posted.
- 11-09-2011, 09:47 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: Phone book arrayList problem
i switched the class names around and when i put PhoneBook myPhoneBook= new PhoneBook(null, null) and that seemed to make the first 2 entries work but not the others. still getting same error
- 11-09-2011, 09:49 PM #8
Re: Phone book arrayList problem
Please post your code and the full text of the errors.
- 11-09-2011, 09:58 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: Phone book arrayList problem
Java Code:package PhoneBookProgram; import java.util.ArrayList; import javax.swing.JOptionPane; public class PhoneBookDemo { /** * @param args */ public static void main(String[] args) { PhoneBook myPhoneBook = new PhoneBook(null, null); ArrayList <PhoneBook> entries = new ArrayList<PhoneBook>(); entries.add( new PhoneBook("Robert","555-555-555")); entries.add( new PhoneBook("Jim","478-124-7235")); entries.add( new Phonebook("Greg", "478-454-7605")); entries.add( new Phonebook("Greg", "478-454-7605")); entries.add( new Phonebook("Greg", "478-454-7605")); for (int x = 0; x < entries.size(); x++){ } } }Java Code:package PhoneBookProgram; 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; } }
errors:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Phonebook cannot be resolved to a type
Phonebook cannot be resolved to a type
Phonebook cannot be resolved to a type
at PhoneBookProgram.PhoneBookDemo.main(PhoneBookDemo. java:16)
thanks again!!!
- 11-09-2011, 10:05 PM #10
Re: Phone book arrayList problem
Look very closely at those two lines of code and see why the first is OK and the second gives an error.Java Code:entries.add( new PhoneBook("Jim","478-124-7235")); entries.add( new Phonebook("Greg", "478-454-7605"));
- 11-09-2011, 10:07 PM #11
Re: Phone book arrayList problem
Whoops, too slow.
- 11-09-2011, 10:23 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: Phone book arrayList problem
Oh gosh!!! thanks soo much guys now that is all done, when i go to display my output i used this line of code, but it seems to be wrong:
JOptionPane.showMessageDialog(null, entries.toString());
- 11-09-2011, 10:25 PM #13
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: Phone book arrayList problem
i get this output :
[PhoneBookProgram.PhoneBook@19821f, PhoneBookProgram.PhoneBook@addbf1, PhoneBookProgram.PhoneBook@42e816, PhoneBookProgram.PhoneBook@9304b1, PhoneBookProgram.PhoneBook@190d11]
[PhoneBookProgram.PhoneBook@19821f, PhoneBookProgram.PhoneBook@addbf1, PhoneBookProgram.PhoneBook@42e816, PhoneBookProgram.PhoneBook@9304b1, PhoneBookProgram.PhoneBook@190d11]
[PhoneBookProgram.PhoneBook@19821f, PhoneBookProgram.PhoneBook@addbf1, PhoneBookProgram.PhoneBook@42e816, PhoneBookProgram.PhoneBook@9304b1, PhoneBookProgram.PhoneBook@190d11]
[PhoneBookProgram.PhoneBook@19821f, PhoneBookProgram.PhoneBook@addbf1, PhoneBookProgram.PhoneBook@42e816, PhoneBookProgram.PhoneBook@9304b1, PhoneBookProgram.PhoneBook@190d11]
[PhoneBookProgram.PhoneBook@19821f, PhoneBookProgram.PhoneBook@addbf1, PhoneBookProgram.PhoneBook@42e816, PhoneBookProgram.PhoneBook@9304b1, PhoneBookProgram.PhoneBook@190d11]
- 11-09-2011, 10:29 PM #14
Re: Phone book arrayList problem
Your code calls the class's toString() method which defaults to the Object class's method since you did not define an override.
The Object class's toString method prints out some data about the object: its name and hashcode. Not too useful.
If you want to see your data for the class, override the toString() method and return a String with what you want to see.
- 11-09-2011, 10:29 PM #15
Re: Phone book arrayList problem
Nothing wrong at all. You are seeing the results of the toString method inherited from Object. If you do not like that output and want something else then you should override the toString method in the PhoneBook class.
- 11-09-2011, 10:52 PM #16
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: Phone book arrayList problem
how do i override a toString method?
public String toString(){
return PhoneBook;
or
public String toString(){
return "Phone book entries " + entries;
- 11-09-2011, 10:53 PM #17
Re: Phone book arrayList problem
Try both and see what happens.
- 11-09-2011, 10:53 PM #18
Re: Phone book arrayList problem
The best way is to try them and see what you get.
I think there is an echo in here.
- 11-09-2011, 11:05 PM #19
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: Phone book arrayList problem
haha i really appreciate the help guys :) i used this:
public String toString(){
return "Phone book entries " + name + number;
and it worked!! i really appreciate everything!!!
- 11-09-2011, 11:07 PM #20
Similar Threads
-
Phone number stringtokenizer problem
By jacques5309 in forum New To JavaReplies: 10Last Post: 11-14-2011, 02:12 AM -
Java Error cannot be applied to (java.lang.String), phone book entry program.
By iceyferrara in forum New To JavaReplies: 12Last Post: 09-23-2011, 01:25 PM -
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
Array Phone book
By Cdlove in forum New To JavaReplies: 3Last Post: 05-06-2010, 06:00 PM -
how to sort "name" arraylist with its phone & address?
By anthrax in forum New To JavaReplies: 2Last Post: 02-02-2009, 11:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks