Well, you first have to find a way to get the contents of that .txt file, and put it in some kind of array. Here is my best method to make that:
class ParseText extends File {
private Scanner readSelf;
private String content;
public ParseText(String filename) {
super(filename);
readSelf = new Scanner(this);
while(readSelf.hasNextLine()) {
content += readSelf.nextLine();
}
readSelf.close();
}
public String[] getEntries() {
ArrayList<String> entries;
while(true) {
int firstBreak = content.indexOf('\n');
boolean notLast = (firstBreak != -1);
// ternary operator
entries.add(content.substring(0, notLast ? (firstBreak - 1) : content.length() - 1));
//more ternary operators
content = notLast ? (content.substring(firstBreak + 1)) : "";
if(content.length() == 0) {break};
}
return entries.toArray(new String[0]);
}
public static HashMap<char, int> getCharacterHashMap(String string) {
HashMap<char, int> charHash = new HashMap<char, int>();
for(char c : string.toCharArray()) {
if(charHash.containsKey(c)) {
int old = charHash.get(c);
charHash.replace(c, old, old + 1);
}
else {
charHash.put(c, 1);
}
}
return charHash;
}
}
Next, an algorithm. We should start to use hashmaps here, since we want to make an un-scrambler for Strings. Each HashMap object should have characters as keys, and integers as values.
class Unscramble {
private static String[] entries = new ParseText("entries.txt").getEntries();
public static match(String someGibberish) {
Object gibberishHash = ParseText.getCharacterHashMap(someGibberish);
ArrayList<int> passes = new ArrayList<int>();
for(int i = 0; i < entries.length; i++) {
HashMap<char, int> currentHash = ParseText.getCharacterHashMap(entries[i]);
char[] keyArray = currentHash.keySet().toArray();
boolean[] fits = new boolean[keyArray.length];
for(int j = 0; j < keyArray.length; i++) {
if(gibberishHash.get(keyArray[j]) != null && currentHash.get(keyArray[j]) <= gibberishHash.get) {
fits[j] = true
}
else {
//breaking to speed up running
break;
}
}
boolean passesEntry = true;
for(int k = 0; k < fits.length; i++) {
if(!(fits[k])) {
passesEntry = false;
break;
}
}
if(passesEntry) {
passes.add(i);
}
}
}
}
I haven't tested this for any errors, so just comment to inform me of any errors. You might also be able to speed this up more with threads, but this should be enough.