如何检查给定的字符串是否为字
你好我正在开发一个文字游戏,我想检查用户输入作为有效的字或不 请建议我可以检查给定的字符串在Android中的方式。如何检查给定的字符串是否为字
例如, String s =“asfdaf” 我想检查它是否有效。
if(s.equals("word from dictionary in loop"){
//action
}
,它也是很好的
s = s.toLowerCase();
所以就不管“宠物小精灵”是怎样的词条
有很多可能的解决方案,这有些是以下
使用Web字典API
http://developer.dictionary.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发现当地的单词列表系统检查单词
使用这个即时通讯没有得到内部条件......它不工作..... – 2014-10-02 13:19:22
@mohammedalisunasara我修好了,它应该工作现在 – zeitue 2014-10-02 20:05:52
兄弟它仍然没有工作,它直接发送返回false – 2014-10-09 07:04:34
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操作系统上,但如果有人知道下面
评论你可以试试这个代码用于基本验证
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();
}
}
}
首先,从例如下载单词列表。将其放置在项目的根目录中。使用下面的代码来检查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);
}
}
你需要有针对该字典...然后进行匹配.. – 2012-07-23 06:14:51
你所说的“有效意思一”?那是不是意思,给定的单词有意义还是不行?你在应用程序中使用字典吗? – AnujAroshA 2012-07-23 06:15:28
在android中您可以有字典形式的文件或SQLite数据库.. – 2012-07-23 06:16:42