Inspired by Reddit r/dailyprogrammer
Help! My keyboard is broken, only a few keys work any more. If I tell you what keys work, can you tell me what words I can write?
The program works fine, not really any additions I wish to make to it. Was just wanting some general feedback on the layout, structure and efficiency.
Note: The dictionary used can be found in the given link.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
/**
* Created on 7/1/2016.
*
* Inspired by r/dailyprogrammer
* https://www.reddit.com/r/dailyprogrammer/comments/3pcb3i/20151019_challenge_237_easy_broken_keyboard/
*/
public class BrokenKeyboard {
private static List<String> wordDict;
private static final String dictPath = "Dictionaries/EnglishWords.txt";
/**
* The main acquires input from the user then
* applies it to the other below methods.
* @param args Unused.
*/
public static void main(String args[]) {
BrokenKeyboard.setDict(dictPath); // Stores dictionary in list.
System.out.println("Processing...");
System.out.println(String.join(", ", findMatches("bikn", wordDict)));
}
/**
* Used to load a given text file into memory.
* @param filePath Used to specify the path of the file that needs to be loaded into memory.
*/
private static void setDict(String filePath){
try {
wordDict = Files.readAllLines(new File(filePath).toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Used to compare a given key to the text file loaded into memory.
* @param key The inputted set of characters compared to the given dictionary.
* @param dict The list of words that will be used to compare to the key.
* @return The list of matches made when comparing the key with the given dictionary.
*/
private static List<String> findMatches(String key, List<String> dict){
List<String> matchList = new ArrayList<>();
for(String word : dict){
String temp = word; // Stores word so that it can be manipulated.
for(char character : key.toCharArray()) {
temp = temp.replace(Character.toString(character), "");
}
if(temp.equals("")){
matchList.add(word);
}
}
return matchList;
}
}