我拿了一個文本文件並將其放入一個數組中。但是,我想在數組內搜索一個String(單詞)。在我的情況下,我想使用掃描儀從用戶獲得輸入,然後在數組中搜索字符串匹配。如何才能完成這樣的任務?陣列文本文件中的搜索字符串
public static void main(String[]args) throws IOException
{
//Scanner scan = new Scanner(System.in);
//String stringSearch = scan.nextLine();
List<String> words = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
reader.close();
System.out.println(words);
}
我的輸出:
[CHAPTER I. Down the Rabbit-Hole, Alice was beginning to get very tired of sitting by her sister on the, bank, and of having nothing to do:]
他正在添加沒有拆分的總行數。改用String.contains –