What's a good way to dynamically allow shortened commands?
I have a BufferedReader set up that attaches to System.in.
so, when I check the BufferedReader, it will retreive any commands put in by a user.
I will have a hash table for commands set up like so:
"north", "dn"
"west", "dw"
"south", "ds"
"east", "de"
"follow", "follow"
"flee", "flee"
"flip", "flip"
So, what I want is to be able to check all the commands and shorten them to the lowest amount of chars before they're the same.
Therefore, for the example hash list, if I typed in "fle", the code would select "flee". Or, if I typed "fo", the code would select "follow". But, if I typed in "f", then the code would respond with a "command not found" error, because "f" could be follow, flip or flee. I want to make it so that I can type "follow", "follo", "foll", "fol", "fo", and "fo" to select the same "follow" command.
Has anyone found a good way of going about this? If my description is confusing, I'll try to re-explain in greater detail.
[1026 byte] By [
zensunnia] at [2007-11-26 18:43:52]

Demo: I'm using the nul character to represent the end of the word, so that the data structure can represent that "hell" and "hello" are both in the vocabulary:
import java.util.*;
class Node {
private SortedMap<Character, Node> children = new TreeMap<Character, Node>();
//0 <= index <= word.length()
private void add(String word, int index) {
if (index == word.length()) {
children.put(Character.valueOf('\u0000'), null);
} else {
char ch = word.charAt(index);
Node child = children.get(ch);
if (child == null) {
children.put(ch, child = new Node());
}
child.add(word, index+1);
}
}
public void add(String word) {
if (word == null || word.length()==0)
throw new IllegalArgumentException();
add(word, 0);
}
public String toString() {
return children.toString();
}
}
public class Example {
public static void main(String[] args) throws Exception {
Node root = new Node();
root.add("hello");
root.add("how");
root.add("who");
root.add("hell");
System.out.println(root.toString());
}
}
Sry for the late reply..
private SortedMap<Character, Node> children = new TreeMap<Character, Node>();
doesn't work in 6.0, I guess. Is there any other way to say: "<Character, Node>" ?
What's the workaround for 6.0?
[edit] I've even tried compiling in the command line terminal with no luck.
[edit again] nevermind - It was just a version setting. works now, but I haven't built my own. yet