> You need to declare a constructor that accepts an array of integers.
> Right now, you only have a parameter-less constructor...you can either
[quoted text clipped - 7 lines]
>
> -Greg
On 19 Feb, 11:26, psm...@mcwy.com wrote:
> > You need to declare a constructor that accepts an array of integers.
> > Right now, you only have a parameter-less constructor...you can either
[quoted text clipped - 7 lines]
>
> > -Greg
Ok - I see where I was going wrong.
I have done that, but when I now go and run the line:
aGame = new Nim(counters);
my response displayed is:
Nim@125b750 - which I thought should have had 7, 9, & 11 displayed.
so when I go and write a method to check the numbers - I get another
mismassed reply of [I@13b8f62
Is there something else I am missing?
Thanks in advance
Lew - 20 Feb 2007 02:16 GMT
> I have done that, but when I now go and run the line:
>
[quoted text clipped - 8 lines]
>
> Is there something else I am missing?
You should post code snippets that are simple, self-contained compilable
examples, what folk call "SSCCE"s. (Examples that show compiler errors are
actually non-compilable, but should generate the exact error message under
consideration.) In your example it would help us to see the code that
generates the output that you describe.
You haven't shown us the part of the code that displays the "response",
presumably a "System.out.println()" or similar. Undoubtedly you fed the array
variable directly to that part of the code:
System.out.println( aGame );
Read the Javadocs for the println() method. You will see that for an arbitrary
Object it uses the argument's "toString()" method. That method shows the class
name, the '@' symbol and a number that identifies the object, unless you
override it.
So what you saw, "Nim@125b750" is the default toString() for your Nim class.
"[I@13b8f62" is the toString() for the array. The arcane symbol "[I" is the
Java identifier for the int array class, just like "Nim" was the identifier
for the Nim class.
You could write a Nim.toString() that loops through the array and concatenates
each array element.
- Lew
uknowwhoiyam@gmail.com - 20 Feb 2007 04:29 GMT
On Feb 19, 8:05 am, psm...@mcwy.com wrote:
> I have done that, but when I now go and run the line:
> aGame = new Nim(counters);
> my response displayed is:
> Nim@125b750 - which I thought should have had 7, 9, & 11 displayed.
You're using Nim.toString(), either implicitly or explictly and you're
getting the default toString().
Either override toString() in class Nim or provide some
other method for returning the numbers as a String:
public String toString()
{
String result = "";
for (int i=0; i<counters.length; i++)
result += String.format("%s%d", (i==0)?"":", ", counters[i]);
return result;
}