Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Question 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

    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++){
    	
    	
    	}
    	
    	
    	
    	}
    
    }
    here is my other one:
    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

  2. #2
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Phone book arrayList problem

    i keep getting the error of PhoneBook cannot be resolved to a type
    You need to have a class named PhoneBook with a constructor with the args that you are calling it with.

    It looks like you have either misnamed the classes or put the constructor you need in the wrong class.

  4. #4
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  7. #7
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Phone book arrayList problem

    Please post your code and the full text of the errors.

  9. #9
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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!!!

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Phone book arrayList problem

    Java Code:
    entries.add( new PhoneBook("Jim","478-124-7235"));
    entries.add( new Phonebook("Greg", "478-454-7605"));
    Look very closely at those two lines of code and see why the first is OK and the second gives an error.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Phone book arrayList problem

    Whoops, too slow.

  12. #12
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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());

  13. #13
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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]

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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.

  16. #16
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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;

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Phone book arrayList problem

    Try both and see what happens.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  19. #19
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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!!!

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Phone book arrayList problem

    Quote Originally Posted by Norm View Post
    I think there is an echo in here.
    Great minds think alike!

Page 1 of 2 12 LastLast

Similar Threads

  1. Phone number stringtokenizer problem
    By jacques5309 in forum New To Java
    Replies: 10
    Last Post: 11-14-2011, 02:12 AM
  2. Replies: 12
    Last Post: 09-23-2011, 01:25 PM
  3. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  4. Array Phone book
    By Cdlove in forum New To Java
    Replies: 3
    Last Post: 05-06-2010, 06:00 PM
  5. Replies: 2
    Last Post: 02-02-2009, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •