【发布时间】: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