像Autocorrect这样的代码需要Java帮助

时间:2016-02-03 01:58:44

标签: java android matcher autocorrect

我正在编写一个与Auto Correct相反的程序。逻辑是用户输入句子,当按下按钮时,应显示与用户输入的句子相反的语法。我几乎能够得到代码。我使用了匹配器逻辑。但我无法获得所需的输出。我正在将代码与此问题联系起来。有人可以帮帮我吗?

stdin

1 个答案:

答案 0 :(得分:0)

By looking at your code, I've found some redundancy and unused variable. Shown as below.

 String store = input;
 String store1 [] = store.split(" ");

As shown below, I did some cleanup for you and implement your logic using Map interface. The wrong value must be the Key of the map so that we can easily determine is the word a wrong value using Map.containKeys(word). If key is found then we concatenate the output variable with the correct word.

    String input = editText.getText().toString().split(" ");

    Map<String, String> pairs = new HashMap<>();
    pairs.put("is", "was");
    pairs.put("shall", "should");
    pairs.put("this", "that");
    pairs.put("can", "could");
    pairs.put("will", "would");

    String output = "";

    for (String word : input) {
        if (pairs.containsKey(word)) {
            output = output + pairs.get(word) + " ";
        } else {
            output = output + word + " ";
        }
    }

    mTextView.setText(output.trim());