Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Java Programs/uniquewords.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.io.*;
//import java.util.Arrays;
import java.util.HashSet;

//Finds unique words in a text file using Hashset
public class UniqueWords {
public static void main(String[] args) throws Exception {
String line;
FileReader file = new FileReader("src/main/resources/file.txt");
BufferedReader br = new BufferedReader(file);
HashSet<String> uniqueWords = new HashSet<>();
while ((line = br.readLine()) != null) {
//array to store the words in the text file
String[] wordsArray = line.toLowerCase().split("\\W");
//uniqueWords.addAll(Arrays.asList(wordsArray));
for(String word : wordsArray)
{
//adding words in the array to Hashset to avoid duplicates
uniqueWords.add(word);
}
}
br.close();

//printing each word in te Hashset uniqueWords
for (String s : uniqueWords)
System.out.println(s);
}

}