Thrasos Website

Sometimes I write about tech and code, and sometimes I don't.

Menu
  • Home
  • About
Menu

java – Create a Dictionary from a text file.

Posted on April 30, 2015March 5, 2016 by thrasos

It takes a text file as input and outputs all the words in that file in alphabetical order, one word per line, ignoring duplicates.

[cc lang=”java” tab_size=”2″ lines=”105″]
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);

}

}
[/cc]

Category: code

Post navigation

← Java – How to add a spell checker to Swing GUI text components JOrtho Library and Wikitionary Dictionaries
Peter Sunde’s Talk at the Transmediale 2015 →

1 thought on “java – Create a Dictionary from a text file.”

  1. Faisal says:
    March 9, 2018 at 4:42 pm

    need to remove the punctuations too like work! or happy,

    Reply

You can say something here... Cancel reply

  • GitHub
  • LinkedIn
  • Twitter
  • Telegram
  • Music with the Phone’s Keypad – Tetris – Part 2May 14, 2023
  • Cleaning up VCARD contacts without a phone.January 29, 2023
  • Docker Compose Notes #2December 15, 2022
  • Docker Notes #1December 14, 2022
  • A.I generated poem in GreekDecember 11, 2022
  • A.I. Generated ArtDecember 10, 2022
  • A.I. generated art.December 9, 2022
  • MailCow, simple html in maindomainSeptember 10, 2022
  • CamouflageFebruary 1, 2022
  • Kerkhoffs principleNovember 16, 2020
© 2023 Thrasos Website | Powered by Minimalist Blog WordPress Theme