我已經差不多完成了這個任務,但還沒有完成。所以我需要的是例如我給出單詞DOG,程序將查看一個文本文件並返回DOG和GOD,也就是說可以由僅給出的可能性生成的單詞。我的代碼給了我所有包含'D','O'和'G'的單詞。我的代碼是這樣的:將給定單詞與相同大小的組合匹配的Java
public class JavaReadTextFile {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ReadFile rf = new ReadFile();
String filename = "/Users/Elton/Desktop/OSWI.txt";
String wordinput;
String wordarray[] = new String[1];
System.out.println("Input Characters: ");
wordinput = input.nextLine();
wordarray[0] = wordinput;
System.out.println(wordinput.length());
try {
String[] lines = rf.readLines(filename);
for (String line : lines) {
if (line.matches(wordarray[0] + ".*")) {
System.out.println(line);
}
}
} catch (IOException e) {
System.out.println("Unable to create " + filename + ": " + e.getMessage());
}
}
}
-----然後我有:
public class ReadFile {
String [] cName = new String [100];
public String[] readLines(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
cName[0] = line.split(" ")[0];
lines.add(cName[0]);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
}
讓我知道你對我的答案有什麼想法,我們應該找到一個解決方案。請致電 – user9349193413 2014-08-31 11:47:28
[SSCCE](http://sscce.org)。 – djechlin 2014-08-31 12:59:22