Java String - See if a string contains only numbers and characters not words?
filter, java, string
Solution
What you need is a dictionary of English words. Then you basically scan your input and check if each token exists in your dictionary. You can find text files of dictionary entries online, such as in Jazzy spellchecker. You might also check Dictionary text file.
Here is a sample code that assumes your dictionary is a simple text file in UTF-8 encoding with exactly one (lower case) word per line:
public static void main(String[] args) throws IOException {
final Set<String> dictionary = loadDictionary();
final String text = loadInput();
final List<String> output = new ArrayList<>();
// by default splits on whitespace
final Scanner scanner = new Scanner(text);
while(scanner.hasNext()) {
final String token = scanner.next().toLowerCase();
if (!dictionary.contains(token)) output.add(token);
}
System.out.println(output);
}
private static String loadInput() {
return "This is a 5gse5qs sample f5qzd fbswx test";
}
private static Set<String> loadDictionary() throws IOException {
final File dicFile = new File("path_to_your_flat_dic_file");
final Set<String> dictionaryWords = new HashSet<>();
String line;
final LineNumberReader reader = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(dicFile), "UTF-8")));
try {
while ((line = reader.readLine()) != null) dictionaryWords.add(line);
return dictionaryWords;
}
finally {
reader.close();
}
}
If you need more accurate results, you need to extract stems of your words. See Apache's Lucene and EnglishStemmer
Problem
I have an array of string that I load throughout my application, and it contains different words. I have a simple if statement to see if it contains letters or numbers but not words . I mean i only want those words which is like `AB2CD5X` .. and i want to remove all other words like `Hello 3` , `3 word` , `any other` words which is a word in English. Is it possible to filter only alphaNumeric words except those words which contain real grammar word. i know how to check whether string contains alphanumeric words ``` Pattern p = Pattern.compile("[\\p{Alnum},.']*"); ``` also know ``` if(string.contains("[a-zA-Z]+") || string.contains([0-9]+]) ```