• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to return the right String value

 
Pierre Bungle
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gentlemen, I thank you.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pierre Bungle wrote:Gentlemen, I thank you.

You are most welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic