This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable and getValueAt() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable and getValueAt()" Watch "JTable and getValueAt()" New topic
Author

JTable and getValueAt()

Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Hello,

I am experimenting with the getValuAt() method. When I use the following:


... I get what I expect... Row2-Column1 ...ect.

However, when I save to file using:


... and then read it back in, the object doesn't seem to be the same type of object.
When I use System.out.println to view out put I get something like [Ljava.lang.Object;@1e4457d,
and I can't cast it to String.

What do I need to do to figure out how to use the data returned from file?

Here is the code not including the file read and write methods.

Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Geoff Jefferson wrote:Here is the code not including the file read and write methods.


Heh, I'd say that's the opposite of what we need. We only need to see the code you're using to read and store the data.

By the way, is there any particular reason you're doing your looping backwards instead of counting up from 0?
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Okay, I'll work on it some more.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

You're writing an Object[] to file and that's what you get back.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Geoff Jefferson wrote:[Ljava.lang.Object;@1e4457d,

That's an Object[]. You can tell:
- [ indicates it's an array
- Ljava.lang.Object; indicates it's an array of java.lang.Object; likewise, [Ljava.lang.String; would be a String[]
- @ is part of Object.toString()
- the remainder is the hexadecimal hash code of the array


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Thanks Rob, your explantion solved my problem.

G.
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
...until now. When I use the array as an argument to a method and then us Object.toString()

I still get [Ljava.lang.Object;@e24e2a.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Line 43 is your problem?

You have an array of Object[] objects. You iterate through that array and yes, each of the elements of the array is an Object[] object. Which is exactly what you are seeing. No surprise there.

You just seem to have assumed that the for-loop, when applied to an array of arrays, would automatically drill down to the lowest level. You're making things too complicated if that's what you assumed. A for-loop, when applied to an array of X, always iterates over each X in the array. If X happens to be Object[] -- as in this case -- then that's what it is. Nothing different happens.
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
>> You're making things too complicated if that's what you assumed.

I do have a tendancy to make things more difficult.
I re-wrote the loop and everything is working as exected.

Thank you.
G.
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Please bear with me. Even though I got the results I was looking for in this example. I am still a little unclear
on what my output was someting like ... [Ljava.lang.Object;@1e0bc08

What is the a representation of the index of the array?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Geoff Jefferson wrote:Please bear with me. Even though I got the results I was looking for in this example. I am still a little unclear
on what my output was someting like ... [Ljava.lang.Object;@1e0bc08

What is the a representation of the index of the array?


I'm sorry, I think I need an English translation of that last sentence.

However, picking out a bit I can understand: "the index of the array". There's nothing in that output which has anything to do with indexes of any array. What you have there is simply a representation of an object which happens to be of the type Object[].
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Why couldnt I cast it to string?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Geoff Jefferson wrote:Why couldnt I cast it to string?


You are calling toString on an array, not on a String. Remember that a 2-d array of anything is an array of arrays. So if you want to see what the row is holding, you have to either loop through the sub array or use Arrays.toString to see it:
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTable and getValueAt()
 
Similar Threads
JTable
JTable
JTable
JTable problem
Reading array from file