arrays - Java: Word search program, what am I doing wrong? -
i've been working word search program while now. ittakes text file input such as:
7 //number of rows 15 // number of columns mucatpoltqfegkq hfytpnsdlhcorey pgrhdqsypyscped gkagdntorioapje yerjodxnqzztfmf hypmmgoronkzhuo qrtzaulhtgtqaao and looks word user enters. file reading , array creation takes place in separate class.
now, need make find words horizontally left right, downwards, , diagonally upper left lower right. i'm trying first find first letter occurs , start evaluating rest of word position.
what i've done works sometimes. i'm able find "cat" vertically on first row, when try find pizza diagonally out of bounds error. know means going beyond array, , know how fix in more simple programs(like for-loop goes through array), not here.
i haven't started on checkdown method because i'd problems have figured out. here's code:
import java.util.scanner; public class wordsearch { private char[][] array; private string targetword; private int rowlocation; private int collocation; public wordsearch(char[][] inarray) { array = inarray; (int row = 0; row < inarray.length; row++) { (int col = 0; col < inarray[row].length; col++) { system.out.print(inarray[row][col]); } system.out.println(); } system.out.println(); } public void play() { scanner input = new scanner(system.in); system.out.println("what word search for? type end quit: "); targetword = input.nextline(); system.out.println("typed in: " + targetword); system.out.println(); comparefirst(targetword); } public void comparefirst(string inword) { (int row = 0; row < array.length; row++) { (int col = 0; col < array[row].length; col++) { if(array[row][col] == inword.charat(0)) { rowlocation = row; collocation = col; suspectanalysis(); } } system.out.println(); } } public void suspectanalysis() { checkright(); checkdown(); checkdiagonal(); } public void checkright() { for(int = 1; < (targetword.length()); i++) { if(array[rowlocation][collocation + i] == targetword.charat(i)) { system.out.println(targetword + " found horizontally @ row " + rowlocation + " , column " + collocation); } } } public void checkdown() { //code goes here } public void checkdiagonal() { for(int = 1; < (targetword.length()); i++) { if(array[rowlocation + i][collocation + i] == targetword.charat(i)) { system.out.println(targetword + " found diagonally @ row " + rowlocation + " , column " + collocation); } } } } i'd appreciate help. thank you!
your checkdiagonal() method going outofbounds because u have not added condition check whether [rowlocation+i] , [collocation+i] in bounds of array. add condition , you'll go.
Comments
Post a Comment