Distinct words in a given file



Programs

package com.candidjava;

import java.io.*;
import java.util.*;

public class Unique
{
	public static void main(String[] args)
	{
		try
		{
			FileReader in = new FileReader("D:/b.txt");
			BufferedReader br = new BufferedReader(in);
			Set<String> uniqueWords = new HashSet<String>();
			String[] words;
			String line;
			while ((line = br.readLine()) != null)
			{
				words = line.split("[-!~\\s]+");
				for (String word : words)
				{
					uniqueWords.add(word);
				}

			}

			System.out.println(uniqueWords);
		}
		catch (Exception e)
		{
			System.out.print("Error");
		}
	}
}


Output

[four, Morning, one, Good, two, three, five]





Related Post

Comments


©candidjava.com