for .. if... else
I am trying to iterate over an array, once i have found an index that is still empty i wish to add something to it and then send that index elsewhere (No longer need to iterate over the block). I have the code which doesn't work below and get an error of method not returning a value. Could someone point me in the right direction pls
private int generateMove()
{
for(int i = 0; i<9; i++)
{
if(gameBoard == ' ')
{
gameBoard = 'O';
if(!isWinner('O') && !isFullBoard())
{
toClient.println(i);
}
}
}
}
Thanks
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
> and get an error of method not returning a value
Well, then return a value. Your method signature says you are going to return and "int" value. Where is your return statement?
Your code does not reflect your intention - and that's the problem.
Invoking toClient.println(i) does not return a value which you expect to happen BUT it passes on the int i to another object as parameter; Your int i variable does not know something about your array BECAUSE it doesn't relate to any of the array object at all. It came from your for-loop variable.
Pass-in the size of the iterator to your generateMove() method before checking against your array - which I suppose is encapsulated in the isWinner() method.
BTW, you will need to provide a more accurate code that you really use in order to get a more accurate help.