Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check if counter is out of bounds
    text
    copied!<p>I am trying to traverse through a 2d array. I currently have a crossword puzzle set up in a char array and I need to be able to check a letter's surrounding letters to see if the word I'm currently looking for is connected to that first letter. I have boolean methods already set up to do a job I need but I would like to know if there is a way to check if my current counter is out of bounds of my array. My array looks like this:</p> <pre><code> W V E R T I C A L L R O O A F F L S A B A C R I L I A T O A N D O D K O N W D C D R K E S O O D D K O E E P Z E G L I W M S I I H O A E R A A L R K R R I R E R K O D I D E D R C D H E L W S L E U T H </code></pre> <p>My current code is this:</p> <pre><code> public static boolean check(int row, int col, String word, char[][] puzzle) { if(checkRight(row, col, word, puzzle)) return true; if(checkLeft(row, col, word, puzzle)) return true; if(checkUp(row, col, word, puzzle)) return true; if(checkDown(row, col, word, puzzle)) return true; if(checkNW(row, col, word, puzzle)) return true; if(checkNE(row, col, word, puzzle)) return true; if(checkSW(row, col, word, puzzle)) return true; if(checkSE(row, col, word, puzzle)) return true; return false; } private static boolean checkRight(int row, int col, String word, char[][] puzzle) { //Checking right direction for(int letter = 1; letter &lt; word.length(); letter++) { if(puzzle[row][col + letter] != word.charAt(letter)) { return false; } } return true; } private static boolean checkLeft(int row, int col, String word, char[][] puzzle) { //Checking left direction for(int letter = 1; letter &lt; word.length(); letter++) { if(puzzle[row + letter][col] != word.charAt(letter)) { return false; } } return true; } private static boolean checkUp(int row, int col, String word, char[][] puzzle) { //Checking up direction for(int letter = 1; letter &lt; word.length(); letter++) { if(puzzle[row - letter][col] != word.charAt(letter)) { return false; } } return true; } private static boolean checkDown(int row, int col, String word, char[][] puzzle) { //Checking down direction for(int letter = 1; letter &lt; word.length(); letter++) { if(puzzle[row][col - letter] != word.charAt(letter)) { return false; } } return true; } private static boolean checkSE(int row, int col, String word, char[][] puzzle) { //Checking diagonals direction for(int letter = 1; letter &lt; word.length(); letter++) { if(puzzle[row + letter][col + letter] != word.charAt(letter) &amp;&amp; &lt;row&lt;) { return false; } } return true; } private static boolean checkSW(int row, int col, String word, char[][] puzzle) { //Checking diagonals direction for(int letter = 1; letter &lt; word.length(); letter++) { if(puzzle[row + letter][col - letter] != word.charAt(letter)) { return false; } } return true; } private static boolean checkNW(int row, int col, String word, char[][] puzzle) { //Checking diagonals direction for(int letter = 1; letter &lt; word.length(); letter++) { if(puzzle[row - letter][col - letter] != word.charAt(letter)) { return false; } } return true; } private static boolean checkNE(int row, int col, String word, char[][] puzzle) { //Checking diagonals direction for(int letter = 1; letter &lt; word.length(); letter++) { if(puzzle[row - letter][col + letter] != word.charAt(letter)) { return false; } } return true; } } </code></pre> <p>Any help would be immensely appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload