• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to return the right String value

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gentlemen, I thank you.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pierre Bungle wrote:Gentlemen, I thank you.


You are most welcome
 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic