3
\$\begingroup\$

I am looking for feedback on a solution to the following problem posed from a book that I'm working through (Java: How To Program, 9th Edition):

Write an application that reads a five-letter word from the user and produces every possible three-letter string that can be derived from the letters of that word. For example, the three-letter words produced from the word "bathe" include "ate," "bet," "tab," "hat," "the," and "tea."

I have a sneaking suspicion that I've over complicated things. Is my code easy to understand? I'm a rookie coder still going over the basics.

import java.util.Scanner;

public class ThreeLetterStrings {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner( System.in );
        System.out.println( "Please enter a five letter word" ); 
        String userInput = sc.nextLine();           // get input
        int wordLength = userInput.length();       // get length of string in question
        char[] charArray = new char[ wordLength ];
        int stem = 0;       // stem being the 2nd letter after the first - b(a)the or ba(t)he
        int scan = 0;       // scan count for while loop
        boolean stamp;

        for( int i = 0; i < charArray.length; i++ )     // feed string into char array
        {
            charArray[ i ] = userInput.charAt( i );
        }

        for( int startLetter = 0; startLetter < wordLength; startLetter++ )
        {
            for( int stemLetter = 1; stemLetter < wordLength; stemLetter++ )
            {
            stem = startLetter + stemLetter;
            if( stem >= wordLength )
                stem = stem - wordLength;
            scan = 0;       // reset scan count after walk for loop
                for( int walk = 0; walk < wordLength - 2; walk++ )
                {
                    System.out.printf( "%c", charArray[ startLetter ] );
                    System.out.printf( "%c", charArray[ stem ] );
                    stamp = false;      // determines whether a character was printed

                    while( stamp == false )
                    {
                        if( scan == startLetter || scan == stem  )
                        {
                            scan++;
                        }
                        else
                        {
                            System.out.printf( "%c", charArray[ scan ] );
                            stamp = true;
                            System.out.println();
                            scan++;
                        }
                    } // end while
                } // end walk for
            } // end stemLetter for
        } // end startLetter for    
    }
}
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Your program can be simplified to something like this:

System.out.println("Please enter a five letter word"); 
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();

for (int i = 0; i < input.length(); i++) {             // pos. of 1st letter
    for (int j = 0; j < input.length(); j++) {         // pos. of 2nd letter
        for (int k = 0; k < input.length(); k++) {     // pos. of 3rd letter
            if (i == j || i == k || j == k) continue;  // any letter taken twice? -> skip
            System.out.printf("%c%c%c\n", input.charAt(i), input.charAt(j), input.charAt(k));
        }
    }
}

Some more pointers:

  • while this works for combinations of three letters, for combinations of more letters (or arbitrary numbers of letters) you should use some sort of recursive function
  • you might want to check whether the entered word is actually five letters long
  • before printing, you could store the three-letter-words in a Set<String> to filter out duplicates
  • the question is a bit unclear whether it's about three-letter strings or three-letter words; in the latter case you might want to get some dictionary of valid three-letter words and check whether the combinations are in that list
\$\endgroup\$
1
  • \$\begingroup\$ Wow! Great stuff a triple nested loop. This is perfect, thank you tobias_k, this is really much simpler. I'm still a couple of chapters away from recursion and collections; still earning my wings. Thanks again, very helpful. \$\endgroup\$ Jul 23, 2013 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.