word search in java 2d array
Solution 1
The return statement goes outside of the for loop, otherwise you will only highlight the first letter.
...
for (int row = j-k+1; row <=i; row++ )
{
//gb.highlight() just calls a method that highlights the letters.
gb.highlight(row, j);
//return true;
}
return true;
...
Solution 2
for (int j = 0; j < letters[i].length; j++)
instead of
for (int j = 0; j < letters.length; j++)
Related videos on Youtube
Wes Johnson
Updated on September 14, 2022Comments
-
Wes Johnson over 1 year
I am trying to create a simple word search for a class assignment, and I have managed to figure out how to search east (from left to right) and west(right to left). But I am having trouble trying to figure out how to search south (top to bottom).
The code that I have works for one file that I read in but the second file returns an
ArrayIndexOutOfBoundsException
. Is there anything that is specific in my code that would make it un-scalable?My corrected code looks like this:
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import wordSeek.GameBoard; public class WordGame { private char[][] letters; GameBoard gb; public static void main(String[] args) { WordGame wg = new WordGame(); wg.play(); } public WordGame() { letters = readLettersFromFile(); gb = new GameBoard(letters); } private void play() { Scanner s = new Scanner(System.in); String word; do { System.out.println("Enter word to find: "); word = s.next(); // reset all highlighted tiles gb.reset(); search(word); } while (!word.equals("QUIT")); gb.dispose(); } // Nothing to be done above // Complete all the methods below private char[][] readLettersFromFile() { // From the data in the file Letters.txt determine the size (number of // rows and number of columns) for the letters array int rowCount = 0; int colCount = 0; char c; File file = new File("resources/Places.txt"); Scanner fileScanner = null; try { fileScanner = new Scanner(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } ArrayList<String> data = new ArrayList<String>(); while(fileScanner.hasNextLine()) { String line = fileScanner.nextLine(); data.add(line); } fileScanner.close(); rowCount = data.size(); colCount = data.get(0).trim().length()/2+1; // Instantiate a two dimensional array of characters of the appropriate // size letters = new char [rowCount][colCount]; // Populate the array with the letters in Letters.txt for (int i = 0; i < rowCount; i++) { String line = data.get(i); int pos = 0; for (int j = 0; j < colCount; j++) { letters[i][j] = line.charAt(pos); pos += 2; } } // return the array return letters; } private void search(String word) { System.out.println("Searching for " + word); //Call the other search methods below as needed searchIterativeEast(word); searchIterativeWest(word); searchIterativeSouth(word); searchIterativeNorth(word); } //The following four methods must employ ITERATION to search the game board private boolean searchIterativeEast(String word) { int k = 0; for (int i = 0; i < letters.length; i++) { for (int j = 0; j < letters[i].length; j++) { if (word.charAt(k) == letters[i][j]) { k++; } else { k = 0; } if (k == word.length()) { for (int col = j - k + 1; col <= j; col++) { gb.highlight(i, col); } return true; } } } return false; } private boolean searchIterativeWest(String word) { String reversedWord = ""; for (int i = word.length() - 1; i != -1; i--) { reversedWord += word.charAt(i); } int k = 0; for (int i = 0; i < letters.length; i++) { for (int j = 0; j < letters[i].length; j++) { if (reversedWord.charAt(k) == letters[i][j]) { k++; } else { k = 0; } if (k == reversedWord.length()) { for (int col = j - k + 1; col <= j; col++) { gb.highlight(i, col); } return true; } } } return false; } private boolean searchIterativeSouth(String word) { int k = 0; int store = letters[0].length; for (int j = 0; j < letters[store].length; j++) { for (int i = 0; i < letters.length; i++) { if (word.charAt(k) == letters[i][j]) { k++; } else { k = 0; } if (k == word.length()) { for(int row = i-k+1 ; row <= i; row++) { gb.highlight(row, j); } return true; } } } return false; } private boolean searchIterativeNorth(String word) { String reversedWord = ""; for (int i = word.length() - 1; i != -1; i--) { reversedWord += word.charAt(i); } int k = 0; int store = 0; for(int i = 0; i < letters.length; i++) { store = letters[i].length; } for (int j = 0; j < letters[store].length; j++) { for (int i = 0; i < letters.length; i++) { if (reversedWord.charAt(k) == letters[i][j]) { k++; } else { k = 0; } if (k == reversedWord.length()) { for(int row = i-k+1 ; row <= i; row++) { gb.highlight(row, j); } return true; } } } return false; }
The Gameboard for the first file (Animals.txt) looks like: 5X4 2d Array
X C A T P A L Q I R B U G P X N G O D W
Output highlights CARP.
The Gameboard for the second file (Places.txt) looks like: 11x15 2d Array
O M J G D A X V C S Q N K I F D A X V T Q O M J H A A H F C A Y W U R P N L F E I T A L Y J N H N E T H E R L A N D S F D B I Z X V T O A R Q O A Y K M K I A H F K R N O D B N N I N Z Y W P H T V C G C T A A N R A Q O T S N L E K K I C M G I H P U U F D C A Z D O X R D X W O A L E U Z E N E V N E O V S U S J R Q L I Z A R B G M
-
Rohit Jain about 11 yearsWhat is letters? Is it a n X n matrix? Or just an array of array?
-
Rohit Jain about 11 yearsOk, here's a hint. You outer loop should iterate through columns, and inner loop should iterate through rows. It's just the reverse of how you normally iterate through an array of array. So, just try and change
letters[i][j]
toletters[j][i]
. And I'm sure you will get an exception then. You need to resolve that exception. -
Rohit Jain about 11 yearsChange
letters[store].length
tostore
in the outer loop condition. -
Wes Johnson about 11 yearsThat fixed the problem. Thank you!
-
-
Wes Johnson about 11 yearsthat fixed the ArrayIndexOutOfBoundsException error, I have been playing around with the code so much that it caused the error.
-
Rohit Jain about 11 years@Achintya. This alone will not solve the issue. There is something more in the problem than just changing the loop condition. But, I hope OP to figure it out.