1

I'm trying to return the array of String but I keep returning [Ljava.lang.String;@1909752 as my answer. Could experts please kindly provide some suggestions to solve this? Thanks in advance!

public String[] getCardNames() 
    {
        String[] namesInHand = new String[CARDS_IN_HAND];
        for (int i =0; i < CARDS_IN_HAND; i++)
        {
            if (hand[i] != null)
            {
               namesInHand[i] = hand[i].toString();
            }

        }
        return namesInHand;
    }
  • 3
    That is the default `toString()` implementation of `Object`, which `String[]` is a subclass of. You have to iterate over the array and print each element. – zeronone Apr 22 '17 at 07:45
  • You're *returning* the `String[]` just fine, the problem is how you're outputting it. See the linked question's answers for how to output the value you're getting from `getCardNames`. – T.J. Crowder Apr 22 '17 at 07:48
  • Oh no wonder, so this where the problem lies! Thank you so much everyone!! – ExtremeBeginner Apr 22 '17 at 08:02

1 Answers1

1

That is the expected behaviour for printing an array. If you want it in a human readable format, call Arrays.toString():

System.out.println(Arrays.toString(getCardNames()))
Sweeper
  • 213,210
  • 22
  • 193
  • 313