I would suggest that the cause of your problems is that checkForWin
is in fact not working. This could be because:
- your testing of that method is inadequate, or
- the code that is updating the file is not doing the right thing (e.g. it isn't closing / flushing the file), or
- you are calling
checkForWin
for the wrong file.
Either way, there is not sufficient information in your Question to say what is actually going on here. At a minimum we need to see the code of the checkForWin
method, and probably we need to see the code that updates the file as well.
While I have your attention ... there are a couple of minor errors in your code ... but not sufficient to cause the problem you asking about:
Your hasWinner
flag is redundant, as is the code that sets it and tests it. The way you have written the loop, the only way you can get to the statements after the loop is if you executed one of the two break
statements.
This is bad style ... and potentially dangerous (in other contexts):
... while (hasWinner == false);
That should be written as
... while (!hasWinner);
Firstly, it is more readable. Every Java programmer should know what the !
operator means, and using !
is the idiomatic way to express this.
Secondly, your approach is error prone. Consider this:
... while (hasWinner = false);
Here you've accidentally written =
instead of ==
. Unfortunately, the =
form is legal Java ... and it means something different to what you intended. If you use the idiomatic version, you can't make this mistake.