【问题标题】:Duplicate Book Titles using Exceptions使用例外重复书名
【发布时间】:2021-12-31 04:21:33
【问题描述】:

我得到了一个文件,我必须找到重复项并将它们放入一个新的文本文件中。这就是我要完成的工作的要点。以下是我得到的指示: 您的客户拥有一家书店,您会发现附有;一个名为 Samsbooks.txt 的文本文件 书店里所有书籍的书名。将所有重复的标题写入并打印到名为 SamsDuplicate.txt。 示例输出:

Duplicate Books
Sam’s Bookstore 2021
Jack and Jill
Peter Pan
My Little Pony

这是我的代码:

enter code here

//In this program, I will write and print all the duplicate book titles to a new file called 
SamsDuplicate.txt.

import java.io.*;

 public class bookstore {

     public static void main(String[] args) throws IOException {

        //Printwriter object for the output file that is called SamsDuplicate.txt.
        PrintWriter duplicates = new PrintWriter("SamsDuplicate.txt");
    
        //Bufferreader object for the input file that is called SamsBookstore.txt.
        BufferedReader original = new BufferedReader(new 
FileReader("C:\\Users\\patti\\Desktop\\Patricks dcom101class\\CSIT 
 210\\SamsBookstore.txt.docx"));
    
    String begin = original.readLine();
    
    //This while loop will read each line of the SamsBookstore.txt file.
    while(begin != null) {
    
        boolean in_stock = false;
        
        //This Bufferreader object is for the output file SamsDuplicate.txt.
        BufferedReader output = new BufferedReader(new FileReader("SamsDuplicate.txt"));
        
        String mid = output.readLine();
        
    //This while loop will read each line of the SamsDuplicate.txt file.
    while(mid != null) {
        
        if(begin.equals(mid)) {
            
            in_stock = true;
            break;
        }
        
        mid = output.readLine();
    }
    //This if statement is if the boolean is false and will also write line from SamsBookstore 
file to SamsDuplicate file.
    if(!in_stock) {
        
        duplicates.println(begin);
    }
        
    begin = original.readLine();
   }
    //Closing both files.
    original.close();
    duplicates.close();
    
    System.out.println("Duplicate Books");
    System.out.println("Sam's Bookstore 2021");
    System.out.println();
    System.out.println();
    }
 }

我不能使用 HashMaps 或任何类似的东西,因为我还没有了解它。我尝试放入最后一个 System.out.println 行:我的打印机(重复项)、我的缓冲区读取器(输出),甚至我创建的名为 SamsDuplicate.txt 的新文件的名称,它们都不会显示重复项。我在这里错过了什么吗?任何帮助将不胜感激!

【问题讨论】:

    标签: java exception duplicates


    【解决方案1】:

    你能用Set吗?

    根据您的代码,Samsbooks.txt 似乎每行都有一个书名,对吧?

    Set 有一个方法add,声明为boolean add(E e)。如果添加了元素,则返回 true;如果未添加,则返回 false,因为它已存在于集合中。

    如果您不能使用Set,您可以通过将每个书名存储在一个数组中来实现类似的功能。您需要首先检查当前书名是否已经在数组中。如果不是,则调整数组大小 +1 并将新书名添加到末尾。

    【讨论】:

    • 是的,文本文件是每行一本书。不,我不能使用 set,我们还没有学会它。将它作为一个数组来给我重复的输出吗?
    • 它是,我从未想过为重复项创建一个数组。当它说“将所有重复的标题写入并打印到一个名为 SamsDuplicate.txt 的文件中”时,我被迷惑了。听起来我应该显示该文件而不是数组。谢谢!
    • @PatrickO'Rourke 在这里可能有所帮助的一件事是:您无法显示文件甚至数组。您只能显示字符串。要“显示数组”,您必须将其转换为某种字符串表示形式,然后显示该字符串。同样,对于文件的内容,您必须获取随后可以显示的字符串。
    • @PatrickO'Rourke 至于解决问题,我建议您考虑如何手动完成。假设我给你一个书籍清单,你需要确定该清单中的重复项。您会采取哪些步骤自己找到它们?
    • @PatrickO'Rourke 我的意思是,如果我给你一张纸上的 100 本书。您会采取哪些步骤手动查找重复项?
    猜你喜欢
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2014-12-12
    • 2010-10-08
    • 2015-06-18
    • 2012-07-31
    相关资源
    最近更新 更多