For a Word Search program, I am prompting the user to enter words, max. 260 and I store their input in an Array List. After the first twenty words being entered, the program asks the user if they would like to add more words (another twenty words). If the user says no, then the program breaks out of the loop and then goes to creating the word search.
The createWordSearch method accepts the word list as a parameter. This is my code for this method:
public static WordArray createWordSearch(List<String> words) {
WordArray wordArr = new WordArray();
// Have numAttempts set to 0 because the while loop below will add one every time we create a new word search
int numAttempts = 0;
while (++numAttempts < 100) { // There will be 100 attempts to generate our word array/grid
Collections.shuffle(words); // The words will be shuffled/randomized
int messageLength = placeMessage(wordArr, "Word Search Puzzle");
int target = arraySize - messageLength;
int cellsFilled = 0;
for (String word : words) { // For each word in the array list 'words'...
cellsFilled += placeWord(wordArr, word);
if (cellsFilled == target) {
// solutions is a list
if (wordArr.solutions.size() >= minWords) { // Minimum number of words to place into the word array/grid to generate = 20
wordArr.numAttempts = numAttempts;
return wordArr;
} else { // We have fulfilled the word array/grid, but we don't have enough words, so we restart (go through the loop again)
break;
}//end of else
}//end of outer if
}//end of for loop
}//end of while loop
System.out.println("Word search has been created.");
return wordArr;
}//end of createWordSearch(words)
where public static final int rows = 10, cols = 10; // Number of rows and columns for the word array/grid AND public static final int arraySize = ( (rows) * (cols));
The methods you see in this method, such as location
and placeWord
go like this:
public static int placeWord (WordArray wordArr, String word) {
int randDirection = rand.nextInt(DIRECTIONS.length);
int randPosition = rand.nextInt(arraySize);
for (int dir = 0; dir < DIRECTIONS.length; dir++) {
dir = ( (dir) + (randDirection) ) % DIRECTIONS.length
for (int pos = 0; pos < arraySize; pos++) {
pos = ( (pos) + (randPosition) % arraySize);
int lettersPlaced = location(wordArr, word, dir, pos);
if (lettersPlaced > 0) {
return lettersPlaced;
}//end of if
}//end of inner for loop
}//end of outer for loop
return 0;
}//end of placeWord(wordArr,word)
public static int location (WordArray wordArr, String word, int dir, int pos) {
int r = ( (pos) / (cols)); // Where r = row
int c = ( (pos) / (cols)); // Where c = column
// Checking the bounds...
if ((DIRECTIONS[dir][0] == 1 && (word.length() + c) > cols)
|| (DIRECTIONS[dir][0] == -1 && (word.length() - 1) > c)
|| (DIRECTIONS[dir][1] == 1 && (word.length() + r) > rows)
|| (DIRECTIONS[dir][1] == -1 && (word.length() - 1) > r)
)
return 0;
int i, cc, rr, overLaps = 0;
// Checking the cells...
for (i = 0, rr = r, cc = c; i < word.length(); i++) {
if (rr < rows && cc < cols) {
return 0;
}//end of if
cc += DIRECTIONS[dir][0];
rr += DIRECTIONS[dir][1];
}//end of for loop
// Placing the word...
for (i = 0, rr = r, cc = c; i < word.length(); i++) {
if (rr < rows && cc < cols) {
overLaps++;
}//end of if
if (i < word.length() - 1) {
cc += DIRECTIONS[dir][0];
rr += DIRECTIONS[dir][1];
}//end of inner if
}//end of for loop 2
int lettersPlaced = ( (word.length()) - (overLaps));
if (lettersPlaced > 0)
wordArr.solutions.add(String.format("%-10s (%d,%d)(%d,%d)", word, c, r, cc, rr));
return lettersPlaced;
}//end of location(wordArr,word,dir,pos)
Where public static final int[][] DIRECTIONS = {{1,0}, {0,1}, {1,1}, {1,-1}, {-1,0}, {0,-1}, {-1,-1}, {-1,1}};
When I debugged my code, I noticed that my program would go through the first if statement in the location method, and then proceed onto the other loops, and then also loop in the placeWord method. The problem is that it is repeating in the loop so many times, and it's not breaking out of the loop. When I clicked "Run" to run my code and chose the option to view the word search, I see that it's empty.
I'm not sure why my program is doing this, and I'm getting super stressed because I'm getting close to being done, but it is due on this upcoming Wednesday morning. I'd highly appreciated it if anyone recommended any solutions or advice me on what to do.
As per your question header, if you want to get out of loop use
break;
and if you want to get out of method usereturn;
get out of loop ex:
get out of method ex: