【发布时间】:2014-10-01 08:14:38
【问题描述】:
给定两个文件
random_letters.txt
AABBBBB
FLOWERS
BACKGFD
TOBEACH
字典.txt
flowers
to
beach
back
我需要检查 random_letters 和字典的每个组合,看看是否有任何共同点。它可以是一个至少有 6 个字符的单词,也可以是两个至少等于 6 个字符的单词。这将使它成为FLOWERS 或TOBEACH。
我很难弄清楚我需要做什么。因为我使用了字符串,所以我可以让它适用于 7 个字符的单词。我知道我需要使用 char 才能使其工作。
到目前为止我所拥有的:
public static void compare() {
private static String stringToWrite2 = "";
private static String[] allWords = new String[2187];
private static String[] diction = new String[79765];
private static char[][] test = new char[2187][7];
private static char[][] test2 = new char[79765][7];
public static void main(String args[])
try {
Scanner file1 = new Scanner(new File("random_letters.txt"));
Scanner file2 = new Scanner(new File("dictionary.txt"));
for(int i = 0; i < 2187; i++) {
allWords[i] = file1.next();
test[i] = allWords[i].toCharArray();
}
for(int i = 0; i < 79765; i++) {
diction[i] = file2.next();
diction[i] = diction[i].toUpperCase();
test2[i] = diction[i].toCharArray();
}
for(int i = 0; i < 2187; i++) {
for (int j = 0; j < 79765; j++) {
if(allWords[i].equals(diction[j])) {
stringToWrite2 += diction[j];
}
}
}
} catch (IOException e) {
System.out.println("could not find file");
}
System.out.println("-------------------");
System.out.println(stringToWrite2);
for(int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++)
System.out.println(test2[i][j]);
}
}}
【问题讨论】:
-
到目前为止您尝试过什么?请阅读How do I ask a good question?。发布您的代码,请参阅How to create a Minimal, Complete, and Verifiable example。
-
代码更容易理解,谢谢!
-
对这些 for 循环迭代进行硬编码是一个非常糟糕的主意......如果从其中一个文件中删除一行,则可能会引发错误。尝试使用 while 循环并读取下一行,只要它在那里。
-
通过“random_letters 的每个组合”,您的意思是 random_letters 中的字符串可以重新排序以匹配 dict 中的单词吗?如果是这样,您需要做很多工作,具体取决于 random_letters 中字符串的长度。
-
@Mshnik 不,他们必须保持相同的顺序
标签: java string char compare converter