首页 > 解决方案 > 为什么我的 Android 设备 Java 键盘无法识别我说的单词?

问题描述

我正在为 Android 设备开发一个键盘来识别某些单词。我目前有一个来自 Envato Tuts+ 的键盘。在为 Android 开发了一个工作键盘之后,我创建了一个名为WordRecognition.java. 它包含使该键盘识别单词所需的所有代码。在我的 Android 设备上测试后,我想知道为什么我无法让它工作。我尝试了所有可能的故障排除方法,但没有一个能解决它。我的代码包含几个方法,我想知道是否有人可以告诉我它有什么问题。这是我的以下代码:

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

在上面的代码中,我Java Scanner在 InvalidWord.txt 文件中搜索单词。我的问题有几种不同的情况,但我认为主要是我的 InvalidWord.txt 文件位于错误的位置。它位于资产文件夹中。如果这不是问题,有人可以修改我的代码来修复它吗?

标签: javaandroidkeyboard

解决方案


推荐阅读