我试图在方法中搜索,编辑和/或删除文本文件中的特定单词
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 + ".");
并为节目输入,然后替换它。
答案 0 :(得分:0)
将(show.equals(searchTerm))
替换为if (show.next().equals(searchTerm))
,搜索就可以了。您实际上是在将Scanner对象与String进行比较。