Generating three-letter strings from five-letter words 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 &lt; charArray.length; i++ )     // feed string into char array
        {
            charArray[ i ] = userInput.charAt( i );
        }

        for( int startLetter = 0; startLetter &lt; wordLength; startLetter++ )
        {
            for( int stemLetter = 1; stemLetter &lt; wordLength; stemLetter++ )
            {
            stem = startLetter + stemLetter;
            if( stem &gt;= wordLength )
                stem = stem - wordLength;
            scan = 0;       // reset scan count after walk for loop
                for( int walk = 0; walk &lt; 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    
    }
}
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 &lt; input.length(); i++) {             // pos. of 1st letter
    for (int j = 0; j &lt; input.length(); j++) {         // pos. of 2nd letter
        for (int k = 0; k &lt; input.length(); k++) {     // pos. of 3rd letter
            if (i == j || i == k || j == k) continue;  // any letter taken twice? -&gt; 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&lt;String&gt; 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