It takes a text file as input and outputs all the words in that file in alphabetical order, one word per line, ignoring duplicates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import java.io.*; import java.util.*; public class Dictionary { public static void main(String[] args) { try{ FileReader fr = new FileReader("words.txt"); BufferedReader br = new BufferedReader(fr); String s; String word = null; String[] arrayWords; ArrayList aListWords = new ArrayList(); int j = 0;while((s = br.readLine()) != null) { Scanner scan = new Scanner(s); while (scan.hasNext()) { word = scan.next().toLowerCase(); aListWords.add(word); j++; } } br.close(); fr.close(); removeDuplicates(aListWords); Collections.sort(aListWords); int size = aListWords.size(); for(int i = 0; i < size ; i++){ System.out.println( aListWords.get( i ) ); } System.out.println(j); } catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } public static void removeDuplicates(ArrayList aList) { HashSet h = new HashSet(aList); aList.clear(); aList.addAll(h); } } |
need to remove the punctuations too like work! or happy,