Jump to content
xisto Community
Sign in to follow this  
Jeune

How Do I Create An Array Of Objects? The code in the tutorial I have read returns an error upon compilation

Recommended Posts

I want to create an array of type Cards. Is this the right way of doing it?

Card decker[];decker = new Card[5];


I always get the error DeckofCards: java:47: missing return statement whenever I compile this.

Share this post


Link to post
Share on other sites

It would help if you posted the whole method that that's in but a "missing return statement" error generally means that you have a method with a defined return type but you never actually return anything in the method. For example:

public static int sum(int a, int b) {	 int c = a + b;}
The method has code in it but nothing is returned (in this case it should be c). The code you posted should work, but I usually put the [] on the type of object as opposed to the variable (e.g., Card[] decker), though I don't think that it matters.

Share this post


Link to post
Share on other sites

I think there is a problem with your creation of the array of Card type. The correct syntax of creating an array would be something of the following String[] aryStrTest; And lets not forget the part in instantiating the array so it would be something like aryStrTest = new String[20]; The thing about arrays is that it has a fixed size, so you really have to know how many elements are you going to expect, alternatively you could use a linklist class which accepts no boundaries with regards to the number of elements. But for that, you have to code it yourself.As for your DeckofCards: java:47: missing return statement error goes, beeseven is right, you lack a return statement, I am thinking that the method you created required you to return a variable.Have fun! I hope you have solved your problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.