pratnala pratnala - 3 years ago 106
Java Question

Find the unique words in a text file

I'm writing this program in Java to find the unique words in a text file. I want to know if this code is correct as it shows even spaces as words.

String[] words;
List<String> uniqueWords = new ArrayList<String>();
words = str1.split("[!-~]* ");
for (int i = 0; i < words.length; i++)
if (!(uniqueWords.contains (words[i])))

For example, if my input is "Hello world! How is the world?" my output array/set/list should have hello, world, how, is, the

Answer Source

Slightly modified version of other answers (I like it short and simple):

String[] words = str1.split("[!-~]* ");
Set<String> uniqueWords = new HashSet<String>();

for (String word : words) {

Note: if you want to split on ! or - or ~ or space, you should use this:

String[] words = str1.split("[-!~\\s]+");

(the dash must be first or last)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download