I have a word list containing 50,000 words, and I also have a txt file that finds the alphabet character line by line. I am trying to find the words containing 7 different letters by reading the words in the word list in order, and I wrote a method for this.
First, I browse the words and sync a character list, then I check each other in the word by navigating the alphabet txt file and increase the counter
if there is. In this way, I try to understand how many different letters are in the words and at the end, if it provides control, I add it to the list.
WordListReader
, read the txt files and return HashSet
.
But it doesn't work that way and returns an empty list. I couldn't find the problem. How can I do that?
public class Pangram {
// Method
public HashSet<String> getPangramList(HashSet<String> wordList) throws IOException {
// Path Alphabets Text File
String alphabetPath = "alphabet.txt";
// Array List and Pangram Word List
ArrayList<Character> allLetters = new ArrayList<>();
ArrayList <Character> ch = new ArrayList<>();
HashSet<String> pangramList = new HashSet<>();
// Characters List
char [] allLettersCharList;
char [] alphaCharacters;
// Alphabets Text Files Reader
WordListReader wordListReader = new WordListReader(alphabetPath);
HashSet<String> alphabetSet = wordListReader.wordReader();
Iterator<String> itr = alphabetSet.iterator();
// Word List
Iterator<String> iterator = wordList.iterator();
// Iterator
while (iterator.hasNext()) {
// Words
String pangramWord = iterator.next();
// Array List Characters
allLettersCharList = pangramWord.toCharArray();
for(int i = 0; i < pangramWord.length();i++){
ch.add(allLettersCharList[i]);
}
int counter = 0;
while (itr.hasNext()){
String alphabet = itr.next();
alphaCharacters = alphabet.toCharArray();
if(ch.contains(alphaCharacters[0])){
allLetters.add(alphabet.charAt(0));
counter++;
}
}
if(counter == 7){
pangramList.add(pangramWord);
}
}
return pangramList;
}
}
Maybe I misunderstood your code but it looks like you do not empty the ‘ch’ variable on each loop round.