• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!