Question: Java Programming: I created this program to remove the duplicate names from a text file. But when I run it, it does not do that.
Java Programming: I created this program to remove the duplicate names from a text file. But when I run it, it does not do that. I. Have tried to see what I can change but I cannot find what is needed to be changed, the result is in the bottom please help me thank you in advance
import java.io.*; import java.util.HashSet; public class RemoveDup { public static void main(String[] args) throws IOException { HashSet people = new HashSet<>(); File file = new File("C:\\Users\\pimpd\\OneDrive\\Desktop\ ames.txt"); //Using buffered reader to read the names.txt file BufferedReader br = new BufferedReader(new FileReader(file)); String temp; System.out.println("Names in file: "); //here we will add all the names to the HashSet while((temp = br.readLine()) != null){ people.add(temp); System.out.println(temp); } System.out.println(); br.close(); System.out.println("People: "); //use for loop to print all the names with no duplicates. for(String str:people) System.out.println(str); } } result:
Names in file:
Pedro
Juan
Rosa
Greg
Pedro
People:
Pedro
Pedro
Juan
Rosa
Greg
Process finished with exit code 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
