0

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
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • what does createWordSearch(words); do? does it perhaps rerun the function? – Joelgullander Jan 06 '19 at 21:38
  • @joelgullander createWordSearch(words) creates the word search grid/array. I shuffle/randomize the 'words' list and, for each word in the word list, I fulfill the word search grid//array until there are enough words in there (20). I return the word search grid/array called wordArr in this method. – Yashvi Shah Jan 06 '19 at 21:45
  • 1
    Java != Javascript – GBlodgett Jan 06 '19 at 21:58
  • 1
    Hello and welcome to StackOverflow! Looks like your original question is solved isn't it? As for , that would be a **[different question](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions "at Stack Exchange sites, “chameleon questions” are not quite welcome")**, consider posting it separately. Please also look at [How to ask good questions](https://stackoverflow.com/help/how-to-ask) to find pointers on how to distill your question to the core problem. – Yolgie Jan 06 '19 at 23:28
  • 1
    Please also look at [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Yolgie Jan 06 '19 at 23:33

1 Answers1

1

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

  • I fixed that, thank you. But now, in the createWordSearch method, I am having a NPE from the for-each loop...I`ve edited my question and put the code snippet for this method as well. – Yashvi Shah Jan 06 '19 at 22:31
  • I tested the code and printed out all the values in the words list and I got all null values. That's what's causing the null pointer exception. –  Jan 06 '19 at 22:47
  • But I am inputting the values for the words list, and THEN create the word search, so I`m quite sure that the input words should still be in the words list... – Yashvi Shah Jan 06 '19 at 22:49
  • What I did to fix the problem was not use the wordList array at all. Right from the beginning, I added values to the words list. Also, for the if statement, a shorter way to achieve what you want is to do is: if ((i + 1) % 20 == 0 && i != 0). –  Jan 06 '19 at 22:59
  • What do you mean "I added values to the words list?" Did you get the input for the list of words inside the createWordSearch method? – Yashvi Shah Jan 06 '19 at 23:02
  • Currently, you're using input.nextLine() to get an input word from the user and then adding that to the wordList by using wordList[i] = input.nextLine(). What I did was add that input word directly to the words list by using words.add(input.nextLine()). –  Jan 06 '19 at 23:06