こんにちは私は、ユーザー入力が有効な単語であるかどうかを確認したいワードゲームを開発しています。Androidで指定された文字列を確認する方法を提案してください。
例:String s="asfdaf"有効かどうかを確認したい。
こんにちは私は、ユーザー入力が有効な単語であるかどうかを確認したいワードゲームを開発しています。Androidで指定された文字列を確認する方法を提案してください。
例:String s="asfdaf"有効かどうかを確認したい。
これには多くの可能な解決策があります。いくつかは次のとおりです
Web Dictionary API を使用する
https://developer.oxforddictionaries.com/
http://googlesystem.blogspot.com/2009/12/on-googles-unofficial-dictionary-api.html
ローカル ソリューションを希望する場合
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 システムで見つかったローカルの単語リストを使用して、単語をチェックします。
まず、例から単語リストをダウンロードします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);
}
}
私は辞書を保存し、そこで検索を行います。その単語が辞書に存在する場合、それは有効です。
ここでこれを行う方法についての手がかりを見つけることができます: Android 辞書アプリケーション
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でこれを試したことはありませんが、誰かが下のコメントを知っている場合
if(s.equals("word from dictionary in loop"){
//action
}
そしてそれはまた良いです
s = s.toLowerCase();
だから、どんなに「ポケモン」が見出し語でもありません。
基本的な検証のためにこのコードを試すことができます
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();
}
}
}