【问题标题】:Search modify and delete from text file从文本文件中搜索修改和删除
【发布时间】: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


    【解决方案1】:

    如果将(show.equals(searchTerm)) 替换为if (show.next().equals(searchTerm)),则搜索会起作用。实际上,您是在将扫描仪对象与字符串进行比较。

    【讨论】:

    • 但它仍然不显示搜索词,知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2021-06-08
    • 1970-01-01
    • 2020-05-19
    • 2019-12-23
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多