Java 如何检查给定字符串是否为单词
你好,我正在开发一个文字游戏,我想检查用户输入的文字是否有效 请建议我如何在android中检查给定字符串 例如。字符串s=“asfdaf”Java 如何检查给定字符串是否为单词,java,android,string,dictionary,Java,Android,String,Dictionary,你好,我正在开发一个文字游戏,我想检查用户输入的文字是否有效 请建议我如何在android中检查给定字符串 例如。字符串s=“asfdaf” 我想检查它是否有效。我会储存一本字典并在那里查找。如果字典里有这个词,它就是有效的 您可以在此处找到一些有关如何执行此操作的线索: 这也很好 s = s.toLowerCase(); 因此,无论“口袋妖怪”是一个多么重要的词条,都有许多可能的解决方案,其中一些解决方案如下 使用web字典API 如果您喜欢本地解决方案 import java.io
我想检查它是否有效。我会储存一本字典并在那里查找。如果字典里有这个词,它就是有效的 您可以在此处找到一些有关如何执行此操作的线索: 这也很好
s = s.toLowerCase();
因此,无论“口袋妖怪”是一个多么重要的词条,都有许多可能的解决方案,其中一些解决方案如下 使用web字典API 如果您喜欢本地解决方案
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系统上找到的本地单词列表来检查单词
泽特说,
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
到
我没有在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();
}
}
}
首先,下载一个单词列表,例如。将其放在项目的根目录中。使用以下代码检查a是否为单词列表的一部分:
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);
}
}
import java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.Collections;
导入java.util.HashSet;
导入java.util.Set;
公共类词典
{
私有集合词集;
公共字典()引发异常
{
Path=Path.get(“words.txt”);
byte[]readBytes=Files.readAllBytes(路径);
String wordListContents=新字符串(readBytes,“UTF-8”);
String[]words=wordListContents.split(“\n”);
wordsSet=newhashset();
Collections.addAll(wordsSet,words);
}
公共布尔包含(字符串字)
{
返回单词set.contains(单词);
}
}
你需要有一本字典。。。然后做一个匹配..你说的“有效的一个”是什么意思?这是不是意味着,给定的单词有没有意义?你在应用程序中使用字典吗?在android中,你可以使用文件或sqlite数据库形式的dict。@AnujAroshA我不想使用字典数据库。有没有办法使用内置字典library@Mahesh你有没有尝试过[UserDictionary]()如果没有,我想你可以找到一个有GPL的字典源,就像[UDM]()有没有办法使用Androides的内置字典功能之类的东西我想要,但它只会给我用户定义的单词:(@Mohammedalisunaara我修好了,现在应该可以用了。兄弟,它还没用,直接送回去false@mohammedalisunasara我的代码和我刚写的代码一样,这取决于/usr/share/dict/american english
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();
}
}
}
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);
}
}