Search code examples
javaarraysnullpointerexceptionuser-input

Getting List of Words Input for Word Search Generator


In this program, this user will have the opportunity to generate their own word search. In the beginning of the program, the user will be shown a menu of instructions in which they can choose between these options: 1. Create a word search 2. Print the word search 3. View solutions to the word search 4. Exit the program

When choosing to create a word search, the user is asked to enter words of their choice, line by line. These words will be stored in a 1-D array.

The user will have to enter a minimum of 20 words, maximum at 260. At every batch of 20 words, the user will be asked if they want to add more words. If the user chooses to add more words, the program will prompt him/her to enter more words until they have reached the max number of words.

If they choose to stop adding more words, the program will jump right into converting the 1-D array to an Array List, and then create the word search, which is where I am having a little problem at.

if (choice == 1) {

    System.out.println("Enter words of your choice line-by-line. You can enter a maximum of 260 words (i.e., 10 words per letter)");    

    System.out.println("");

    String[] wordList = new String[260];

    // This for loop will loop around with it`s body the user decides they have added enough words and wish to proceed

    for (int i = 0; i < wordList.length; i++) {

        wordList[i] = input.nextLine();

          if (wordList[i] == wordList[19] || wordList[i] == wordList[39] || wordList[i] == wordList[59] || wordList[i] == wordList[79] || wordList[i] == wordList[99] || wordList[i] == wordList[119]|| wordList[i] == wordList[139]) {

         System.out.print("Do you want to keep adding words? Enter Y/N: ");
                  String answer = input.next().toUpperCase();

               if (answer == "Y") {

                    wordList[i] = input.nextLine();

               } if (answer == "N") {

                    break;

                 }//end of inner if

               }//end of outer if

            }//end of for loop

        List<String> words = Arrays.asList(wordList);

        createWordSearch(words);

When the user types in "N," the program is supposed to end the for loop and jump right into the part after }//end of for loop. Ive added 'break;' in order to stop the loop, but instead Im pretty sure it is stopping the entire program from proceeding. When I remove the 'break;' the program keeps prompting me to add more words (basically until there are 260 words added in total).

EDIT

So, I have fixed the problem up there thanks to the advice Ive received here. The program is doing what I want now, but I am getting aNullPointerExceptionfrom my for-each loop in thecreateWordSearch(words)` method.

public static WordArray createWordSearch(List<String> words){
    WordArray wordArr = new WordArray();    
    // Have numAttempts set to 0 because the while loop below will add one every time we create a new word search       
    int numAttempts = 0;        
    while (++numAttempts < 100) { // There will be 100 attempts to generate our word array/grid     
        Collections.shuffle(words); // The words will be shuffled/randomized            
        int messageLength = placeMessage(wordArr, "Word Search Puzzle");            
        int target = arraySize - messageLength;         
        int cellsFilled = 0;            
        for (String word : words) { // For each word in the array list 'words'...               
            cellsFilled += placeWord(wordArr, word); // NULL POINTER EXCEPTION!!! 'word' is not initialized I`m pretty sure

Solution

  • It might be because you did answer == "N" instead of answer.equals("N").