NewBie - java constructor error can't find constructor

Discussion in 'Java' started by psmith@mcwy.com, Feb 19, 2007.

  1. Guest

    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
    , Feb 19, 2007
    #1
    1. Advertising

  2. Guest

    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


    On 19 Feb, 10:55, wrote:
    > 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
    , Feb 19, 2007
    #2
    1. Advertising

  3. Greg Guest

    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
    Greg, Feb 19, 2007
    #3
  4. Guest

    On 19 Feb, 11:03, "Greg" <> 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
    > 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
    , Feb 19, 2007
    #4
  5. Guest

    On 19 Feb, 11:26, wrote:
    > On 19 Feb, 11:03, "Greg" <> 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
    > > 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
    , Feb 19, 2007
    #5
  6. Lew Guest

    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.
    >
    > 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
    Lew, Feb 20, 2007
    #6
  7. Guest

    On Feb 19, 8:05 am, 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);
    return result;
    }
    , Feb 20, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zimmy
    Replies:
    0
    Views:
    401
    zimmy
    Jul 7, 2004
  2. ad
    Replies:
    2
    Views:
    845
  3. clicwar
    Replies:
    24
    Views:
    720
    clicwar
    Jul 25, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,192
  5. Wybo Dekker
    Replies:
    1
    Views:
    336
    Yukihiro Matsumoto
    Nov 15, 2005
Loading...

Share This Page