aspose file tools*
The moose likes Beginning Java and the fly likes cant read the contents of the printed string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cant read the contents of the printed string" Watch "cant read the contents of the printed string" New topic
Author

cant read the contents of the printed string

jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
Object obj[] = new Object[1];
obj= query.list().toArray();

for (Object x: obj) {
System.out.println(obj[0].toString());}
//[Ljava.lang.Object;@1fd6bea
//[Ljava.lang.Object;@1fd6bea
//[Ljava.lang.Object;@1fd6bea

hi-
i commented out the output to the console.
as you can see that doesnt help me.
up above query.list()[from hibernate] return type is List.

i looked everywhere and i cant figure out
how to stop it from sticking it in a
array element all mashed together in secret code.

i'd like to not only read it but break it apart.
i have fooled with this before and struggled.
i think it is time i wrote the answer down
and stick it in my manual.

please help!
jim
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

The thing that jumps out at me is that you are declaring a variable, x, in your for declaration, but you have hard-coded the value that you are printing out:

For every element in the array, you print out the value of the first element in the array.
As for what is printed out, have a look at the java documentation:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

If you want to see something else from toString(), you will have to override it in whatever class you are expecting back from hibernate.


[How To Ask Questions On JavaRanch]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I agree with what Joe said and I would like to add my thoughts.

The following is your code:



The following is what I think you want to do.



In "new line 1", I only declare an array of objects. I don't instantiate the array since the array of objects reference variable, obj, is given a value in line 2.

In "new line 2", an object reference variable named, query, has a method named list() that returns an object that has a method named toArray() that instantiates an array with the same elements as query.list().

In "new line 3", you are using the for-each loop that came out with the Java Language version 5.0 and later. Here, the object reference variable will be assigned as each of the elements of the array called: obj.

In "new line 4", each value that was in the array of objects is converted to a String. The output that you are seeing is the result of the method of the class String called toString().

Like Joe answered, you need to override the method toString() for the objects that are in the query.list().\
[ January 15, 2008: Message edited by: Kaydell Leavitt ]
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
hi kaydell and joe-

thanks for your reply.
i am quite sure i can get it to work now.

it is very simple.

create a table in apache derby.
generate a pojo class related to the new table.
use my iterators to go through the table data.

part of the query was mapped to a table and its
fields and the rest of the fields were not mapped.

i cant believe i didnt realize this sooner.
i had a funny brain moment(s).

i havent used java in a 1/2 year or so i think.

thanks for encouraging and helping me.
i can say with 99% confidence i will get this now.

jim
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cant read the contents of the printed string