NewBie - java constructor error can't find constructor

P

psmith

I have to declare a private instance variable, that can reference an
array of integers(3 of them).

I then need to take a suitable argument as an array, and then
initialise it to the private variable.

I have put

private int[] controlGroups;

public Nim()
{
this.controlGroups = new int[3];
}

Then when I run my code:

int[] counters = {7, 9, 11};
Nim aGame;
aGame = new Nim(counters);

I get this message:

Semantic error: line 3. Constructor error: Can't find constructor:
Nim( [I ) in class: Nim


Thanks in advance
 
A

asciz

Hi, you need to declare a constructor that takes the int array as an
argument and then sets the private variable, something like this:

private int[] controlGroups;

public Nim(int [] startValue)
{
this.controlGroups = startValue;
}


-asciz
 
G

Greg

You need to declare a constructor that accepts an array of integers.
Right now, you only have a parameter-less constructor...you can either
edit that constructor to accept an argument of type int[], or you can
add another constructor with that method signature.

public Nim(int[] newControlGroups) {
this.controlGroups = newControlGroups;
}

-Greg
 
P

psmith

You need to declare a constructor that accepts an array of integers.
Right now, you only have a parameter-less constructor...you can either
edit that constructor to accept an argument of type int[], or you can
add another constructor with that method signature.

public Nim(int[] newControlGroups) {
this.controlGroups = newControlGroups;

}

-Greg

Thanks very much
 
P

psmith

You need to declare a constructor that accepts an array of integers.
Right now, you only have a parameter-less constructor...you can either
edit that constructor to accept an argument of type int[], or you can
add another constructor with that method signature.
public Nim(int[] newControlGroups) {
this.controlGroups = newControlGroups;

-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
 
L

Lew

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?

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
 
U

uknowwhoiyam

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);
return result;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top