Java - elements in an array problem, help please

Permabanned
Joined
18 Nov 2011
Posts
368
Location
Aberystwyth
Hello, at the moment, the method I'm using currently returns whats left in the array as strings rather than an int count.

so lets say I had 4 cards left in the game it would return

4 diamonds, 5 spades, ace clubs, queen diamonds.

[4d.gif, 5s.gif, ac.gif, qd.gif]

However, I need to add to the method so that it counts those elements left and returns them as an int, which in this case would be 4. That would be the score for the game.

Anyone be able to give me some sort of help on this situation please? If possible with an explanation, not just the answer, as then I won't learn anything, which is the whole purpose. :)

Example

public int size()
{
return graphicCard.size();
}

System.out.println("The number of elements in the array are:" +graphicCard+ "");
 
haven't done java for a while, but presuming all the gifs are roughly the same size surely there is some sort of array function to do something like:

sizeof(array)/sizeof(gif)

Then simply return that?
 
You want to use length i.e. if you had an array called str:

Code:
int count = str.length();

Remember the array is initialised at 0 so if you have 4 cards in your array count = 3.
 
Right, i solved that problem. Now i need to be able to write to file with the score I have obtained.

public void save (String scoreList) throws IOException {
PrintWriter outfile = new PrintWriter (new OutputStreamWriter

(new FileOutputStream(scoreList)));
outfile.println(graphicCard.size());
for (Game c:cards) {
outfile.println(c.toString());
}
outfile.close();
}

So far i have used the following. Any ideas where i've gone wrong. I've put the error in bold. The error stated;

cannot find symbol - variable cards
 
You are trying to iterate over something you haven't declared. I'd suspect it should be:

for (Game c:graphicCard)

I see you posted at gone midnight. Sleep is often the best answer :)
 
You want to use length i.e. if you had an array called str:

Code:
int count = str.length();

Remember the array is initialised at 0 so if you have 4 cards in your array count = 3.

Err no it doesn't

arraylength.jpg


An arrays index might start at 0 but if you have 4 items in the array the length is still 4.

edit: Also note length is a public int of an array, not a public method, hence the lack of ()
 
Last edited:
Back
Top Bottom