5

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 . String s = "asfdaf" i want to check whether its a valid one.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Mahesh
  • 1,257
  • 1
  • 14
  • 24
  • 2
    you need to have a dictionary for that ... and then make a match.. – Harmeet Singh Jul 23 '12 at 06:14
  • What do you mean by "valid one"? Is that mean, the given word has a meaning or not? Are you using a dictionary in your application? – AnujAroshA Jul 23 '12 at 06:15
  • in android you can have dict in form of a file or a sqlite database.. – Harmeet Singh Jul 23 '12 at 06:16
  • @AnujAroshA i dont want use dictionary db. is there any way to use the built in dictionary library – Mahesh Jul 23 '12 at 06:23
  • 1
    @Mahesh have you try with [UserDictionary] (http://developer.android.com/reference/android/provider/UserDictionary.html) If not, I think you can find a source of Dictionary that has GPL, just like [UDM] (http://udm.adrianvintu.com/) – AnujAroshA Jul 23 '12 at 06:51

6 Answers6

8

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

Tyler Dane
  • 951
  • 1
  • 14
  • 25
zeitue
  • 1,674
  • 2
  • 20
  • 45
5

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);
    }
}
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
2

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

Community
  • 1
  • 1
Lucas Kauffman
  • 6,789
  • 15
  • 60
  • 86
1
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

pokeyOne
  • 312
  • 1
  • 2
  • 12
0
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

cyborg86pl
  • 2,597
  • 2
  • 26
  • 43
-1

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


    }

}
kenshinji
  • 2,021
  • 4
  • 25
  • 39
mohit sharma
  • 1,050
  • 10
  • 20