5

こんにちは私は、ユーザー入力が有効な単語であるかどうかを確認したいワードゲームを開発しています。Androidで指定された文字列を確認する方法を提案してください。

例:String s="asfdaf"有効かどうかを確認したい。

4

6 に答える 6

8

これには多くの可能な解決策があります。いくつかは次のとおりです

Web Dictionary API を使用する

https://developer.oxforddictionaries.com/

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

http://www.dictionaryapi.com/

ローカル ソリューションを希望する場合

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

これは、すべての Linux システムで見つかったローカルの単語リストを使用して、単語をチェックします。

于 2012-07-23T06:32:26.803 に答える
5

まず、例から単語リストをダウンロードしますhere。プロジェクトのルート ディレクトリに配置します。次のコードを使用して、aStringが単語リストの一部であるかどうかを確認します。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Dictionary
{
    private Set<String> wordsSet;

    public Dictionary() throws IOException
    {
        Path path = Paths.get("words.txt");
        byte[] readBytes = Files.readAllBytes(path);
        String wordListContents = new String(readBytes, "UTF-8");
        String[] words = wordListContents.split("\n");
        wordsSet = new HashSet<>();
        Collections.addAll(wordsSet, words);
    }

    public boolean contains(String word)
    {
        return wordsSet.contains(word);
    }
}
于 2016-04-09T16:20:03.853 に答える
2

私は辞書を保存し、そこで検索を行います。その単語が辞書に存在する場合、それは有効です。

ここでこれを行う方法についての手がかりを見つけることができます: Android 辞書アプリケーション

于 2012-07-23T06:16:12.757 に答える
1
zeitue は次のように述べています。
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"));
    }
}

ただし、これはLinuxでのみ機能します。Macでこれと同じことが必要な場合は、パスを次から変更します

/usr/share/dict/american-english

/usr/share/dict/web2

Windowsでこれを試したことはありませんが、誰かが下のコメントを知っている場合

于 2015-06-23T16:48:22.740 に答える
0
if(s.equals("word from dictionary in loop"){
    //action
}

そしてそれはまた良いです

s = s.toLowerCase();

だから、どんなに「ポケモン」が見出し語でもありません。

于 2012-07-23T06:16:47.883 に答える
-1

基本的な検証のためにこのコードを試すことができます

import java.util.Scanner;

public class InputValidation {

    public static void main(String[] args) {
        String input;
        try {
            System.out.println("Enter the input");
            Scanner s = new  Scanner(System.in);

            input = s.next();
            if(input.matches(".*\\d.*")){
                System.out.println(" Contains digit only");
            } else{
                System.out.println(" Only String/words found");
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}
于 2015-10-08T14:05:04.930 に答える