aspose file tools*
The moose likes Beginning Java and the fly likes Unable to return the right String value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unable to return the right String value" Watch "Unable to return the right String value" New topic
Author

Unable to return the right String value

Pierre Bungle
Greenhorn

Joined: May 05, 2009
Posts: 9
Hello everyone,

Slight problem with getting a result from a method. I'm returning an 20x20 array of Strings from a very long string, returning this new array to the main, and then printing it. Should be straightforward enough, but my printed result just gives me this:
[[Ljava.lang.String;@696e59da

I've searched around this forum, googled it, but don't really understand what's causing this, or how to remedy it.

Here's the part of the code where I think the problem lies:


When I printed the array directly from my makeArray() method, I didn't have this problem. What I'm looking for is to understand what's going, the particular point here being me getting used to returning results from methods. Am I right in thinking that the reason I'm getting this gobbledygook is that my String[][] a array isn't the right size?

Thanks in advance for any help with this!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You aren't having a problem with returning the value:
[[Ljava.lang.String;@696e59da

See those two brackets on the left side: they mean you have an array of arrays of Strings. And you are printing the outer array's toString() method. So the code does what is expected.

If you want to print the individual values you have to loop through the arrays and print the Strings stored in them.


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Pierre Bungle wrote:Hello everyone,

Slight problem with getting a result from a method. I'm returning an 20x20 array of Strings from a very long string, returning this new array to the main, and then printing it. Should be straightforward enough, but my printed result just gives me this:
[[Ljava.lang.String;@696e59da

I've searched around this forum, googled it, but don't really understand what's causing this, or how to remedy it.

Here's the part of the code where I think the problem lies:


When I printed the array directly from my makeArray() method, I didn't have this problem. What I'm looking for is to understand what's going, the particular point here being me getting used to returning results from methods. Am I right in thinking that the reason I'm getting this gobbledygook is that my String[][] a array isn't the right size?

Thanks in advance for any help with this!



The toString() method for arrays, just prints out that it is an array, the type, and the identity hash. If you are expecting, nicely formatted rows and columns, then you will have to write that yourself -- looping through the array.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Pierre Bungle wrote:
When I printed the array directly from my makeArray() method, I didn't have this problem.

you mean before returning String[][] are you able to see values using *System.out.println*?

Array dont have meaningful toString method. so when you pass this to System.out.println , again println mathod calls array's toString method. so you are seeing the weird result.
you can print array elements in 2 ways,

1. use for loop and print - here you need 2 nested for loop to loop through array elements

2. use Arrays.deepToString()

<edit>Too slow </edit>
Pierre Bungle
Greenhorn

Joined: May 05, 2009
Posts: 9
Gentlemen, I thank you.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Pierre Bungle wrote:Gentlemen, I thank you.

You are most welcome
 
 
subject: Unable to return the right String value