2012-07-23 184 views
3

你好我正在開發一個文字遊戲,我想檢查用戶輸入作爲有效的字或不 請建議我可以檢查給定的字符串在Android中的方式。如何檢查給定的字符串是否爲字

例如, String s =「asfdaf」 我想檢查它是否有效。

+2

你需要有針對該字典...然後進行匹配.. – 2012-07-23 06:14:51

+0

你所說的「有效意思一」?那是不是意思,給定的單詞有意義還是不行?你在應用程序中使用字典嗎? – AnujAroshA 2012-07-23 06:15:28

+0

在android中您可以有字典形式的文件或SQLite數據庫.. – 2012-07-23 06:16:42

回答

2

我會存儲一本字典並在那裏查找。如果該詞出現在詞典中,則該詞有效。

你可以找到關於如何做到這一點這裏一些線索: Android dictionary application

+0

是否有任何方式使用android – Mahesh 2012-07-23 06:27:33

+0

的內置字典功能檢查我添加的鏈接:) – 2012-07-23 06:31:07

+0

yes kind我想要的東西,但它只會給我只有用戶定義的話:( – Mahesh 2012-07-23 06:52:34

0
if(s.equals("word from dictionary in loop"){ 
    //action 
} 

,它也是很好的

s = s.toLowerCase(); 

所以就不管「寵物小精靈」是怎樣的詞條

7

有很多可能的解決方案,這有些是以下

使用Web字典API

http://developer.dictionary.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發現當地的單詞列表系統檢查單詞

+1

使用這個即時通訊沒有得到內部條件......它不工作..... – 2014-10-02 13:19:22

+0

@mohammedalisunasara我修好了,它應該工作現在 – zeitue 2014-10-02 20:05:52

+0

兄弟它仍然沒有工作,它直接發送返回false – 2014-10-09 07:04:34

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/web2 

/usr/share/dict/american-english 

路徑我還沒有嘗試過在Windows操作系統上,但如果有人知道下面

0

評論你可以試試這個代碼用於基本驗證

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


    } 

} 
5

首先,從例如下載單詞列表。將其放置在項目的根目錄中。使用下面的代碼來檢查String是否在詞列表的一部分或不:

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); 
    } 
} 
相關問題