How to check whether given string is a word

前端 未结 6 1106
礼貌的吻别
礼貌的吻别 2020-12-02 00:51

Hello I am developing a word game where i want to check the user input as valid word or not please suggest the way i can check the given string in android.

Eg . Str

6条回答
  •  猫巷女王i
    2020-12-02 01:13

    zeitue said:
    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"));
        }
    }
    

    but this will only work on linux. if you want this same thing on a mac change the path from

    /usr/share/dict/american-english
    

    to

    /usr/share/dict/web2
    

    I have not tried this on windows but if someone knows comment below

提交回复
热议问题