Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

NewBie - java constructor error can't find constructor

Thread view: 
psmith@mcwy.com - 19 Feb 2007 10:55 GMT
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
asciz@starmail.com - 19 Feb 2007 11:02 GMT
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, psm...@mcwy.com wrote:
> I have to declare a private instance variable, that can reference an
> array of integers(3 of them).
[quoted text clipped - 24 lines]
>
> Thanks in advance
Greg - 19 Feb 2007 11:03 GMT
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
psmith@mcwy.com - 19 Feb 2007 11:26 GMT
> 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

Thanks very much
psmith@mcwy.com - 19 Feb 2007 16:05 GMT
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;
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.