4 Replies - 282 Views - Last Post: 15 November 2012 - 09:07 AM Rate Topic: -----

#1 itsjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

ArrayList error

Posted 14 November 2012 - 11:43 PM

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 that i'm having is that when i try to run it it displays something like "PhoneBook@713c817" instead of what i want it to display. it compiles so far.

Here is the two programs!

public class PhoneBook
{
	private String name; 
	private String number; 
	public PhoneBook()
	{
		name = null;
		name = null;
	}
	
	
	public PhoneBook (String personName, String phoneNumber)
	{
		name = personName; 
		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 name; 
	}
	
}



and here is the other(demo) file
import java.util.ArrayList;


public class PhoneBookDemo
{
	public static void main (String [] args)
	{
	
	
	PhoneBook name1 = new PhoneBook();
	PhoneBook name2 = new PhoneBook();
	PhoneBook name3 = new PhoneBook();
	PhoneBook name4 = new PhoneBook();
	PhoneBook name5 = new PhoneBook();
	
	ArrayList<PhoneBook> names = new ArrayList<PhoneBook>();
	
	name1.setName("Bill");
	name1.setNumber("000-000-0000");
	
	name2.setName("Jay");
	name2.setNumber("111-111-1111");
	
	name3.setName("Rachel");
	name3.setNumber("222-222-2222");
	
	name4.setName("Ashley");
	name4.setNumber("333-333-3333");
	
	name5.setName("John");
	name5.setNumber("444-444-4444");
	
	names.add(name1);
	names.add(name2);
	names.add(name3);
	names.add(name4);
	names.add(name5);
	
	for (int x = 0; x < names.size(); x++)
	{
		System.out.println(names.get(x));
	}
	}
}



Any help would be appreciated.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList error

#2 mrleakehe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 31-October 12

Re: ArrayList error

Posted 15 November 2012 - 01:27 AM

I'm still a rookie myself but I believe that implementing an toString method in your phonebook class would solve your problem.

Tip: You can save yourself allot of coding by making use of your constructor. All those separate statements could be condensed into something like:
names.add(new PhoneBook("name", "phone number");

Was This Post Helpful? 1
  • +
  • -

#3 itsjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: ArrayList error

Posted 15 November 2012 - 08:57 AM

Could you show me how I would do that? My book doesn't explain the toString method clearly.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: ArrayList error

Posted 15 November 2012 - 09:01 AM

The toString() is simply what should be used when your Object is in the context that required a String
In your case will simplyh be in your PhoneBook class

public String toString() {
   return name + " Tel: " + number;
}



or whatever you want displayed when a PhoneBook object is used in a println() call
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,105
  • Joined: 19-March 11

Re: ArrayList error

Posted 15 November 2012 - 09:07 AM

View Postmrleakehe, on 15 November 2012 - 03:27 AM, said:

I'm still a rookie myself but I believe that implementing an toString method in your phonebook class would solve your problem.

Tip: You can save yourself allot of coding by making use of your constructor. All those separate statements could be condensed into something like:
names.add(new PhoneBook("name", "phone number");



Good advice from the rookie.

toString is a method inherited from the Object class (which all classes inherit from). It's the method called when the print() method wants to render your object as a String. By default, it prints out a unique identifier for the object composed of the object's class name and some number which is guaranteed to be unique. This is, as you see, usually not what you want.

To replace this behavior, implement a method called toString in your PhoneBook class which composes and returns a String for that object.

For example, if you have a Person object, toString() might look like

public String toString()
{
  return this.name
}


That is, just print the name field of this Person object. You can get as fancy as you like, but generally I use this for debugging so I try to make sure I mention the class and some identifying information. In this case, you'd like to compose whatever String you want to show in the context of your call to print the PhoneBook.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1