patternjavaMinor
Generating three-letter strings from five-letter words
Viewed 0 times
threefivewordsgeneratingletterfromstrings
Problem
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 = 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
{
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 = 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
{
Solution
Your program can be simplified to something like this:
Some more pointers:
System.out.println("Please enter a five letter word");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
for (int i = 0; i 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
Setto 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
Code Snippets
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));
}
}
}Context
StackExchange Code Review Q#28864, answer score: 5
Revisions (0)
No revisions yet.