I'm working on a code for a word game. This method gets as an input a 2D arrays which contains words in lexicographic order, for example words in vocabularyByLetter[0]
all start with an a
.
It also gets three strings each one is a single character (I need it to be string not char). Then it finds all the words that start with firstLetter
then it looks for words that contain the second and third letter, if the third and second are the same letter they should appear twice in the word.
My method goes through a big text but here are some examples:
input : "y" , "e" , "e"
output:
yelled
yourselves
found 2 words
My code returns all the words that start with the first letter, although I check the other statement.
public static void printWords(String[][] vocabularyByLetter,
String firstLetter, String secondLetter, String thirdLetter) {
int firstnum = (((int) firstLetter.charAt((0)) % 97) + 1);
int count = 0;
for (String word : vocabularyByLetter[firstnum]) {
if (secondLetter == thirdLetter) {
int counter = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == secondLetter.charAt(0)){
counter++;
}
}
if (counter==2){
count++;
System.out.println(word);
}
}
if (secondLetter!=thirdLetter){
if (word.contains(secondLetter) && word.contains(thirdLetter)) {
count++;
System.out.println(word);
}
}}
System.out.println("found" + count + "words");
}