【发布时间】:2015-02-06 20:18:22
【问题描述】:
我正在尝试进行方法搜索、编辑和/或删除文本文件中的特定单词
private void modifyShow() throws FileNotFoundException, IOException {
Scanner input = new Scanner(System.in);
System.out.println("Please search for a TV Show\nExample: Simpsons");
String tvSearch = input.nextLine();
System.out.println("Displaying results for: " + tvSearch);
String searchTerm = tvSearch;
searchTerm = searchTerm.toLowerCase();
int count = 0;
Scanner show = new Scanner(new File("src/TVShows.txt"));
while (show.hasNext()) {// loop
if (show.equals(searchTerm)) { // find
System.out.println(show);// display
}
}
}
所以它实际上并没有搜索文件,但我相信它会打开它,但是 while 语句将它扔掉。所以一旦它找到需要打印的单词,就说
System.out.println("Enter a new name for " + tvSearch + ".");
并且有一个节目的输入然后替换它。
【问题讨论】:
标签: java text replace text-files