【发布时间】:2017-07-25 15:06:13
【问题描述】:
我几乎已经做到了这一点,但还没有完全做到。所以我需要的是例如我给出单词 DOG,程序将查看一个文本文件并返回 DOG 和 GOD,即只能通过给定的赔率生成的单词。我的代码给了我所有包含“D”、“O”和“G”的单词。我的代码是这样的:
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()]);
}
}
【问题讨论】:
-
让我知道您对我的回答有何看法,我们应该找到解决方案。
-
SSCCE 请。