How to check whether given string is a word

There are many possible solutions to this some are the following

Use a web Dictionary API

https://developer.oxforddictionaries.com/

http://googlesystem.blogspot.com/2009/12/on-googles-unofficial-dictionary-api.html

http://www.dictionaryapi.com/

if you would prefer a local solution

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class WordChecker {
    public static boolean check_for_word(String word) {
        // System.out.println(word);
        try {
            BufferedReader in = new BufferedReader(new FileReader(
                    "/usr/share/dict/american-english"));
            String str;
            while ((str = in.readLine()) != null) {
                if (str.indexOf(word) != -1) {
                    return true;
                }
            }
            in.close();
        } catch (IOException e) {
        }

        return false;
    }

    public static void main(String[] args) {
        System.out.println(check_for_word("hello"));
    }
}

this uses the local word list found on all Linux systems to check for the word

Leave a Comment