Hello I am developing a word game where i want to check the user input as valid word or not please suggest the way i can check the given string in android.
Eg . Str
There are many possible solutions to this some are the following
Use a web Dictionary API
https://developer.oxforddictionaries.com/
http://googlesystem.blogspot.com/2009/12/on-googles-unofficial-dictionary-api.html
http://www.dictionaryapi.com/
if you would prefer a local solution
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"));
}
}
this uses the local word list found on all Linux systems to check for the word
zeitue said: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")); } }
but this will only work on linux. if you want this same thing on a mac change the path from
/usr/share/dict/american-english
to
/usr/share/dict/web2
I have not tried this on windows but if someone knows comment below
if(s.equals("word from dictionary in loop"){
//action
}
and it's also good to
s = s.toLowerCase();
so there would be no matter how "pokemon" is the entry word
You can try this code for basic validation
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();
}
}
}
I'd store a dictionary and do a lookup in there. If the word is present in the dictionary, it's valid.
You can find a some clues on how to do this here: Android dictionary application
First, download a word list from for example here. Place it in the root directory of your project. Use the following code to check whether a String is part of the word list or not:
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);
}
}