some word games require the player to find words that can be formed using the le
ID: 3683966 • Letter: S
Question
some word games require the player to find words that can be formed using the letters of another word. for example, given the word SWIMMING then on other words that can be formed using the letters include SWIN, WIN, WING, SING, MINING, etc. Write a program that lets the user enter a word and then output all the words contained in the file words.txt that can be formed from the letters of the entered word. one algorithm to do this is to compare the letter histograms for each word. create an array that counts up the number of each letter in the entered word (e.g., one S, one W, two I, two M, etc.) and then creates a similar array for the current word read from the file. the two arrays can be compared to see if the word from the file could be created out of the letters from the entered word.(the file words.txt is very large with thousands of words from the english language. if really needed i could email the document but i need this very soon.)
Explanation / Answer
Answer:
import java.io.*;
import java.util.*;
public class MyMeaningfulWords {
public List<String> getMeaningfulWords(String fileName){
FileInputStream fileinputs = null;
DataInputStream datafileinputs= null;
BufferedReader br = null;
List<String> ListofWords = new ArrayList<String>();
try {
fileinputs = new FileInputStream(fileName);
datafileinputs = new DataInputStream(fileinputs);
br = new BufferedReader(new InputStreamReader
(datafileinputs));
String line = null;
while((line = br.readLine()) != null){
StringTokenizer st = new StringTokenizer(line, " ,.;:"");
while(st.hasMoreTokens()){
String temp_variable = st.nextToken().toLowerCase();
if(!ListofWords.contains(temp_variable)){
ListofWords.add(temp_variable);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{if(br != null) br.close();}catch(Exception ex){}
}
return ListofWords;
}
public static void main(String a[]){
MyMeaningfulWords meanwords = new MyMeaningfulWords();
List<String> ListofWords = meanwords.getMeaningfulWords
("C:/word.txt");
for(String str_variable:ListofWords){
System.out.println(str_variable);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.