0

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?

  • 1) Is this code supposed to run on PC command line or in Android? 2) If you think your problem is reading a text file from assets folder, did you not search and find [Reading a simple text file](https://stackoverflow.com/q/5771366/295004)? Based on your post history, I recommend getting a good working knowledge of Java before tackling Android. – Morrison Chang Jan 13 '19 at 05:01
  • This code is supposed to run on a Android Device. I am using this line of code to read the file. `Scanner file = new Scanner(new File("InvalidWord.txt"));` –  Jan 13 '19 at 18:28
  • But where is "InvalidWord.txt". Android has no concept of 'current directory' since you are delivering pre-packaged compressed software into a mobile device. Fun fact, an APK is just a zip file and not everything is expanded at install time. See: [How can I read a text file in Android?](https://stackoverflow.com/a/12421888/295004) – Morrison Chang Jan 13 '19 at 20:01
  • My text file is in Android Studio. It contains a String variable that stores all of the invalid words. –  Jan 13 '19 at 21:42
  • Based on your last comment it seems that your code no longer matches your question. StackOverflow works best when you have a specific programming problem you are trying to solve. This is not a tutorial site, and SO is not a code writing service. The best advice is still the same from what others have given in your prior questions, learn Java, then learn Android. There are a lot of free resources to do both. Good luck. – Morrison Chang Jan 14 '19 at 03:40

0 Answers0