1

I'm writing a word search program that scans in from a text file. The text file contains two integers (the dimensions of the puzzle, which in this case is 10x10), followed by the puzzle, followed by another integer (the amount of words to find), and finally the words to be found. I am trying to write the code so that the program will execute a for loop which will scan the next line in, then add each character in the string to the 2D array. However, the scanner continues seems to continue through the file and eventually "out of bounds" on the file, even though, according to the for loop, it should run no more than 10 times (the dimension of the puzzle is 10x10)

Here is the problematic code:

Scanner scan = new Scanner(new File("puzzle.txt"));
    int rows = scan.nextInt();
    int columns = scan.nextInt();   
    int [][] wordSearch = new int[rows][columns];

    for (int i = 0; i < rows; i++){
        for (int j = 0; j < columns; j++){
            String temp = scan.nextLine();
            for (int k = 0; k < temp.length(); k++){
                wordSearch[i][j] = temp.charAt(k);

              }
            }
        }

Here is the puzzle.txt:

  • 10 10
  • WVERTICALL
  • ROOAFFLSAB
  • ACRILIATOA
  • NDODKONWDC
  • DRKESOODDK
  • OEEPZEGLIW
  • MSIIHOAERA
  • ALRKRRIRER
  • KODIDEDRCD
  • HELWSLEUTH
  • 10
  • WEEK
  • FIND
  • RANDOM
  • SLEUTH
  • BACKWARD
  • VERTICAL
  • DIAGONAL
  • WIKIPEDIA
  • HORIZONTAL
  • WORDSEARCH
5
  • 1
    What does puzzle.txt look like? If it's not too big please post it Commented Sep 27, 2012 at 20:57
  • Where is this error occurring? Commented Sep 27, 2012 at 20:58
  • 1
    You mean it should run no more than 100 times?? Commented Sep 27, 2012 at 20:58
  • yes; please post an example of the input Commented Sep 27, 2012 at 20:58
  • @Coupon22 At the very end, the error occurs. I tested it, and it seems to run for every line of the code instead of just the ten I wanted it to. Commented Sep 27, 2012 at 21:00

1 Answer 1

3

However, the scanner continues seems to continue through the file and eventually "out of bounds" on the file, even though, according to the for loop, it should run no more than 10 times (the dimension of the puzzle is 10x10)

No, look at your loop:

for (int i = 0; i < rows; i++){
    for (int j = 0; j < columns; j++){
        String temp = scan.nextLine();

You're running it for each row and each column. I suspect you actually want:

for (int i = 0; i < rows; i++) {
    String temp = scan.nextLine();
    for (int j = 0; j < columns; j++) {
        wordSearch[i][j] = temp.charAt(j);
    }
}

Of course, this assumes that you've correctly set the length of each line to the number of columns - you may want to validate that after you read the line.

One thing which should have raise suspicions was this inner loop:

for (int k = 0; k < temp.length(); k++) {
    wordSearch[i][j] = temp.charAt(k);
}

That would overwrite the same wordSearch[i][j] for every character in temp, making all but the last iteration pointless.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the clarification! That seems to have solved the immediate problem, but unfortunately, I now have a string index out of bounds exception on the line that contains wordSearch[i][j] = temp.charAt(j);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.