aspose file tools*
The moose likes Beginning Java and the fly likes Error trying to print array to screen through toString().. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error trying to print array to screen through toString().." Watch "Error trying to print array to screen through toString().." New topic
Author

Error trying to print array to screen through toString()..

Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
I created a person object and added to array through this code..

Person person1 = new Person ("Waria", 6, true);
Greggs.addPerson(person1);


Declared the array to store the person in a seperate class through...

Person employee [] = new Person [50];

And now i am trying to print the users to the screen through the following code:

public void viewMembers()
{
System.out.println(employee.toString());
}


But all i get is "[LPerson;@10b30a7" as the output. Can someone please shed some light on what the problem could be?

Sorry i am new to coding, so i probably didnt make any sense, i'd be happy to explain further if someone didnt understand?

Thanks in advance...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When you call toString() on an array reference, it gives a String representation of the array object itself -- not the contents of the array.

The implementation of toString() is inherited from java.lang.Object, so you can check the API to see why you're getting things like "[LPerson;@10b30a7."

To get the array's contents, you need to iterate through the array and call toString() on each element in the array.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
If you are going to use the toString method, you'll also want to write/override the toString() method for your Person class to format the object's contents in some way. Otherwise you'll get something like you got for the employee array.
[ July 28, 2008: Message edited by: Norm Radder ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Originally posted by marc weber:
To get the array's contents, you need to iterate through the array and call toString() on each element in the array.

Or call java.util.Arrays.toString(employee);


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error trying to print array to screen through toString()..