Looking for help with program please - newbie programmer ;)
Hey all,
I'm new to Java and I'm currently studying it in my spare time. I have a code question that I just can't seem to figure out and hopefully some of the more experienced folks on here can lend a hand.
I'm coding a wordsearch puzzle and I need to write a helper method called isHorizontalSpaceFree()
The method should check whether(starting from aRow and aCol) there is enough free space to insert word into letterGrid(left to right). If there is enough space, the method should return true, otherwise it should return false.
I need the method to return an out of bounds exception if the word length exceeds the end of the array as well.
Here is my code so far
public boolean isHorizontalSpaceFree(int aRow, int aCol, String word)
{
boolean result = true;
if (aCol < NUMBER_COLS - word.length())
{
int i = aCol;
while (aCol < NUMBER_COLS - word.length())
{
if (letterGrid[aRow][aCol] != BLANK_ELEMENT
|| aCol > NUMBER_COLS - word.length())
{
result = false;
}
aCol++;
}
}
else
{
result = false;
}
return result;
}
I hope it's not too far away lol
Thanks in advance :s:
Re: Looking for help with program please - newbie programmer ;)
Please use [code] tags [/code] when posting code.
What does that code do that it shouldn't?
Re: Looking for help with program please - newbie programmer ;)
Hi and thanks for the quick respons. I will adhere to the code posting rules from now on - thanks for the pointer ;)
I'm looking at the code and all I can possibly say is that initialising int i = aCol might be wrong.
I think it may be something wrong with the logic or the if conditions though and this is what I can't work out.
Any futher light shed and help is really appreciated here.
Thanks
Re: Looking for help with program please - newbie programmer ;)
You haven't said what it's doing that it shouldn't, though.
Stick some println()s in there so you can see what's happening inside that code.
Re: Looking for help with program please - newbie programmer ;)
Hi again,
I put in some println()s and it appears to be processing the whole array up to NUMBER_COLS - word.length(). What I need it to do is to check that there is enough space for the word to fit i.e there are enough BLANK_ELEMENT in a row to accomodate the word.length() and for the program to return false if it hits a char() or exceeds the array boundary.
I'm still puzzled lol ;(
Thanks