-1
package assignment2;
import java.util.Random;
import java.util.Scanner;

public class test2 {
public static void main(String[] args) {
    int wordAmount = wordAmount();
    String[] words = wordArray(wordAmount);
    displayWords(words, wordAmount);
    char[][] puzzleGrid = generatePuzzleGrid(words, wordAmount);
    //System.out.println(Arrays.deepToString(puzzleGrid)); //just using this to test
    printPuzzleGrid(puzzleGrid, wordAmount);
}

public static int wordAmount(){
    Scanner input = new Scanner(System.in);
    System.out.print("How many words would you like in the word search(5-15): ");
    int wordAmount = input.nextInt();
    while(wordAmount < 5 || wordAmount > 20){
        System.out.println("The number you've requested is either to large or to small.");
        System.out.print("How many words would you like in the word search(5-20): ");
        wordAmount = input.nextInt();
    }
    return wordAmount;
}

public static String[] wordArray(int wordAmount){
    String[] words = new String[wordAmount];
    Scanner input = new Scanner(System.in);
    for(int i = 0; i < wordAmount; i++){
        System.out.print("Enter a word: ");
        words[i] = (input.nextLine().toUpperCase());
        for(int j = 0; j < wordAmount; j++){
            if(j == i) break;
            while(words[i].contains(words[j])){
                System.out.print("The word you entered has already been entered, enter a new word: ");
                words[i] = (input.nextLine().toUpperCase());
            }
        }
        while(words[i].length() > 10 || words[i].length() <= 2 || words[i].contains(" ")){
            System.out.print("The word you entered has been rejected, enter a new word: ");
            words[i] = (input.nextLine().toUpperCase());
        }
    }
    return words;
}

public static void displayWords(String[] words, int wordAmount){
    System.out.print("The words you must find are: ");
    for(int w = 0; w < wordAmount; w++){
        System.out.print(words[w] + "  ");
    }
    System.out.println("");
}

public static char[][] generatePuzzleGrid(String[] words, int wordAmount){
    char[][] puzzleGrid = new char[wordAmount][wordAmount];
    Random rand = new Random();
    for(int across = 0; across < wordAmount; across++){
        for(int down = 0; down < words[across].length(); down++){
            puzzleGrid[across][down] = words[across].charAt(down);
            for(int filler = wordAmount; filler >= words[across].length(); filler--){
                puzzleGrid[across][filler] = (char)(rand.nextInt(26) + 'A'); //this is the line with the problem
            }
        }
    }
    return puzzleGrid;
}

public static void printPuzzleGrid(char[][] puzzleGrid, int wordAmount){
    for(int across = 0; across < wordAmount; across++){
        for(int down = 0; down < wordAmount; down++){
            System.out.print(" " + puzzleGrid[down][across]);
        }
        System.out.println("");
    }
}    
}

It seems my last problem has worked itself out, but now I face a new problem.

Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at assignment2.test2.generatePuzzleGrid(test2.java:63) at assignment2.test2.main(test2.java:10) C:\Users\Higle\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 9 seconds)

  • Can you explain what `wordAmount` and `words` are exactly, and also what you are trying to achieve in your inner loop? – RaminS Dec 04 '16 at 00:14
  • When you ask about an exception, always post the exact and complete stack trace. It tells exactly what the exception is, and from which code it's thrown. Have you read the documentation of ArrayIndexOutOfBoundsException? Have you used your debugger to find out where it came from? You're trying to access words[across]. Does words contain enough elements? – JB Nizet Dec 04 '16 at 00:15
  • wordAmount is an integer obtained from the user and words is an array of strings obtained from the user –  Dec 04 '16 at 00:18
  • what keeps a word from running off the right edge of the grid? – Jeremy Kahan Dec 04 '16 at 00:18
  • What are the parameters you passed when you get this exception ( word and wordAmount values)? – Coder Dec 04 '16 at 00:22

2 Answers2

1

It seams application runs without any issue only concern is that what will happen if the user provide wordAmount( number of characters in grid ) greater than the word he provided. So it's better to add validation on wordAmount with word length as show on below.

public static char[][] generatePuzzleGrid(String[] words, int wordAmount) {
    if(wordAmount>words.length){
        wordAmount = words.length;
    }
    char[][] puzzleGrid = new char[wordAmount][wordAmount];
    for (int across = 0; across < wordAmount; across++) {
        for (int down = 0; down < words[across].length(); down++) {
            puzzleGrid[across][down] = words[across].charAt(down);/////////////////
        }
    }
    return puzzleGrid;
}
Coder
  • 1,917
  • 3
  • 17
  • 33
0

It looks like you're calling charAt(5) on a string of less than 5 characters. You can do something like

if(words[across].length() < (down-1)){ 
    continue;
}

to make sure you don't get that particular error... Also, you may like to know charAt() returns the index of a char from 0->length()-1

cashc
  • 1