我需要一些帮助,我有一个程序,提示用户输入一个7位数字,然后根据给定的数字生成一个可能的单词组合列表。
所以我宣布了这个2D数组:
public static final String[][] letter ={
{"","",""}, //if user enters 0
{"","",""}, //1
{"A","B","C"}, //2
{"D","E","F"}, //3
{"G","H","I"}, //4
{"J","K","L"}, //5
{"M","N","O"}, //6
{"P","R","S"}, //7
{"T","U","V"}, //8
{"W", "X","Y"} //9
然后我生成了这个方法来运行所有可能的单词/字母组合:
private static void writeOutputToString(){
looper = new int[Utilities.numberLength];
int upper = Utilities.letter[0].length-1, lower =0;
for(looper[0]=lower; looper[0] <= upper; looper[0]++){
for(looper[1]=lower; looper[1] <= upper; looper[1]++){
for(looper[2]=lower; looper[2] <= upper; looper[2]++){
for(looper[3]=lower; looper[3] <= upper; looper[3]++){
for(looper[4]=lower; looper[4] <= upper; looper[4]++){
for(looper[5]=lower; looper[5] <= upper; looper[5]++){
for(looper[6]=lower; looper[6] <= upper; looper[6]++){
for(int count = looper.length-1; count >=0; --count){
list.add(Utilities.letter[numbers[count]][looper[count]]);
counter++;
我目前正在使用stringToWrite + =&#34; \ n&#34 ;;在每个单词出现之后,在一个新的行后,但它会在每个字母之间加上一个逗号,我不确定如何解决它!而list是我声明用于保存单词的ArrayList。 Utilities类保持上面的2D数组!
此外,我正在阅读一个文本文件中,该文本文件将字典中的所有单词一个接一个地(只有一个列)作为一个ArrayList,因为我不知道有多少个值。文件。
private static Scanner x;
private static ArrayList<String> dictionary = new ArrayList<String>();
public static void openFile(){
try{
x = new Scanner(new File("E:\\dictionary.txt"));
}
catch(Exception e){
System.out.println("could not find file");
}
}
public static void readFile(){
while(x.hasNext()){ //going to loop as long as there is something else
dictionary.add(x.next());
}
}
接下来我需要做的是将我生成的单词从7位数字进行比较,然后比较字典中的单词列表。如果任何单词匹配,那么我需要将它们输出到文本文件output.txt。我被困住了,不知道该怎么办,如果有人有任何帮助,或者至少有一些示例代码可以帮我弄清楚要去哪里!
答案 0 :(得分:0)
使用哈希集存储字典条目
HashSet<String> dictionary=new HashSet<String>();
for (String s : Files.readAllLines()) {
dictionary.add(s);
}
然后您可以使用HashSet.contains()
for (String s : list) {
if (dictionary.contains(s)) {
//output to textfile...
}
}