I am developing a keyboard for Android Devices to recognize certain words. I currently have a keyboard from Envato Tuts+. After developing a working keyboard for Android, I created a new class called WordRecognition.java
. It contains all of the code necessary to make this keyboard recognize words. After testing this on my Android Device, I was wondering why I could not get it to work. I tried all of the possible troubleshooting ways, but none of them fixed it. My code includes a couple of methods and I was wondering if someone could please tell me what was wrong with it. This is my following code:
package jt.thinktwice.com.thinktwice;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import static android.content.ContentValues.TAG;
public class WordRecognition {
public static void main(String[] args) throws FileNotFoundException{
// Read the file using whitespace as a delimiter (default)
// so that the input will be split into words
Scanner file = new Scanner(new File("InvalidWord.txt"));
Set<String> InvalidWord = new HashSet<>();
// For each word in the input
while (file.hasNext()) {
// Convert the word to lower case, trim it and insert into the set
// In this step, you will probably want to remove punctuation marks
InvalidWord.add(file.next().trim().toLowerCase());
}
System.out.println("Enter a word to search for: ");
Scanner input = new Scanner(System.in);
// Also convert the input to lowercase
String search = input.next().toLowerCase();
// Check if the set contains the search string
if (InvalidWord.contains(search)) {
System.out.println("Invalid Word Said!");
Log.i(TAG, "main: Invalid Word Said ");
} else System.out.println("Valid Word Said!");
Log.i(TAG, "main: Invalid Word Said ");
}
In the code above, I have the Java Scanner
search for words in my InvalidWord.txt file. I have a couple of different scenarios to my problem, but I think the main one is that my InvalidWord.txt file is on the wrong location. It is in the Assets folder. If that is not the problem, can someone please revise my code to fix it?