0

I have 2 list List<String> existingGuesses which contains characters and List<String> word = new ArrayList<String>(words) which contains words which is a copy of list words as shown in my code below. What I am trying to do is to iterate through all the words of List word and iterate through the characters of list existingGuesses. Then I want to compare the first character of List existingGuesses with all the words of List word one by one and if a match is found then I want to remove the word from the list. Then the same for next character and continue until there are no more words left to compare. Until now, I could only declare a iterator, it's not necessary to use iterator but I don't know any other ways. My code is as follows:

 public List<String> getWordOptions(List<String> existingGuesses, String newGuess) 
   {
    List<String> word = new ArrayList<String>(words);
    String c = existingGuesses.get(0); //trying to get the first character
    ListIterator<String> iterator = word.listIterator(); //iterator to iterate thorugh word list
    while(iterator.hasNext())
    {           
      if(word.contains(c)) //trying to compare
      {
       word.remove(c);
      }
    }

    return null;
  }

Can anybody help me out? The original List words is:

   private List<String> words = new ArrayList<String>() {
    {
        // DO NOT CHANGE THESE WORDS!
        String w =
                "sentence\n"
                + "together\n"
                + "children\n"
                + "mountain\n"
                + "chipmunk\n"
                + "crashing\n"
                + "drinking\n"
                + "insisted\n"
                + "insulted\n"
                + "invented\n"
                + "squinted\n"
                + "standing\n"
                + "swishing\n"
                + "talented\n"
                + "whiplash\n"
                + "complain\n"
                + "granddad\n"
                + "sprinkle\n"
                + "surprise\n"
                + "umbrella\n"
                + "anything\n"
                + "anywhere\n"
                + "baseball\n"
                + "birthday\n"
                + "bluebird\n"
                + "cheerful\n"
                + "colorful\n"
                + "daylight\n"
                + "doghouse\n"
                + "driveway\n"
                + "everyone\n"
                + "faithful\n"
                + "flagpole\n"
                + "graceful\n"
                + "grateful\n"
                + "homemade\n"
                + "homework\n"
                + "housefly\n"
                + "kickball\n"
                + "kingfish\n"
                + "knockout\n"
                + "knothole\n"
                + "lipstick\n"
                + "lunchbox\n"
                + "newscast\n"
                + "nickname\n"
                + "peaceful\n"
                + "sailboat\n"
                + "saturday\n"
                + "shameful\n"
                + "sidewalk\n"
                + "snowball\n"
                + "splendid\n"
                + "suitcase\n"
                + "sunblock\n"
                + "sunshine\n"
                + "swimming\n"
                + "thankful\n"
                + "thinnest\n"
                + "thursday\n"
                + "whatever\n"
                + "whenever\n"
                + "windmill\n"
                + "american\n"
                + "possible\n"
                + "suddenly\n"
                + "airplane\n"
                + "alphabet\n"
                + "bathroom\n"
                + "favorite\n"
                + "medicine\n"
                + "december\n"
                + "dinosaur\n"
                + "elephant\n"
                + "February\n"
                + "football\n"
                + "forehead\n"
                + "headache\n"
                + "hospital\n"
                + "lollipop\n"
                + "november\n"
                + "outdoors\n"
                + "question\n"
                + "railroad\n"
                + "remember\n"
                + "sandwich\n"
                + "scissors\n"
                + "shoulder\n"
                + "softball\n"
                + "tomorrow\n"
                + "upstairs\n"
                + "vacation\n"
                + "restroom";

        addAll(Arrays.asList(w.split("\\s+")));
    }
};
user227666
  • 734
  • 2
  • 18
  • 34
  • It seems you're doing it the right way. While it's possible to use `for each` loop, you will need to create a copy of the original list in the loop but modify the original one. See **all** answers and comments to them in this [SO post](http://stackoverflow.com/questions/1196586/calling-remove-in-foreach-loop-in-java). – PM 77-1 Sep 23 '13 at 17:14

1 Answers1

0

Some points to consider:

   String c = existingGuesses.get(0); //trying to get the first character

gets the first word in a list of words, not the first character.

To get the first character in a String you can do this:

  char c = newguess.getCharAt(0);

Then you can search through the list for words starting with that character. Next you do not want to remove words that contain the character, you want to remove words that start with the character.

Also, you want to get each item from the iterator one at a time:

   while(iterator.hasNext()){
      String w = iterator.next(); //get the next word in the iterator here
      //process w here
   }

You seem to want to find if the characters correspond. That is, if the guess is "foo" then you want to remove all words that start with f, and all words that has the second character o and all words that have the third character o. That seem a little strange so you may want to check your logic.

Your method seem to be referring to some global variables and that could be causing you some confusion.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • you got me wrong, I want to remove word if the character matchs, irrespective first or last or middle – user227666 Sep 23 '13 at 17:29
  • Pick the first character from the list existingGuesses, and go through all the words in the copied list. If the character is found in a word, the word is removed from the copied list. Continue with the next characters until: all the characters have been iterated, and there are still words remaining in the copied list. – user227666 Sep 23 '13 at 17:30
  • OK. Understood. In that case the contains() method will work. – Vincent Ramdhanie Sep 24 '13 at 14:19