1

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");

}
Tam211
  • 723
  • 5
  • 10
  • 27
  • 1
    You should be comparing `String`s with `equals()`. – August Nov 25 '14 at 18:17
  • Please change the title to something more meaningful. Just a suggestion instead of doing all this crazy stuff you can use [startWith(String)](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith(java.lang.String)) – StackFlowed Nov 25 '14 at 18:18
  • The error is here: *It also gets three strings each one is a single character (I need it to be string not char)*. The symptoms of this error are messes like `firstLetter.charAt((0))` and the fact that your program doesn't work. – Dan Bron Nov 25 '14 at 18:23

0 Answers0