【问题标题】:getting the matching words from two string arrays and changing words that are off by one letter从两个字符串数组中获取匹配的单词并更改一个字母的单词
【发布时间】:2018-05-11 05:25:26
【问题描述】:

我有一个 String[] 是 userInput 和另一个 String[] fileInput 是从文件中读取的输入。我正在尝试从userInput 中获取与文件输入中的单词匹配的单词。注意:所有单词只有 3 个字母。 棘手的部分是我必须将仅相差一个字母的userInput 单词更改为fileInput 中的正确单词。 例如,我的字符串数组 fileInput 包含:[cat, dog, run, man,jaw, max] 所以如果 userInput 中的索引之一包含单词 bat 我必须更改它。 任何帮助表示赞赏。

for (int i = 0; i < userInput.length; ++i) {
        if (Arrays.toString(fileInput).contains(userInput[i])) {
            match += userInput[i] + " ";
            userInput[i] = null;
        }


    }
    for (int i = 0; i < userInput.length; ++i) {
        if (userInput[i] != null){

        }
    }

【问题讨论】:

  • 你有没有尝试过?这可以通过传统方法或使用流来完成
  • @phflack 是的,我尝试过 for 循环。但它没有用。我不明白为什么我会投反对票,我只是在问一个问题。你能详细说明一下吗
  • 发布你的代码(downvote 不是我)
  • @phflack 我尝试了很多东西,但我似乎无法弄清楚。这是我现在的代码,但需要一些编辑
  • 用户/文件输入有效,您只是在查找相似词时遇到问题,对吧?

标签: java arrays string string-matching


【解决方案1】:

输入:

  • 用户输入数组(长度为 3 的字符串)
  • 文件输入数组,要检查的字典(长度为 3 的字符串)

输出:

  • 选中的单词数组(可以只修改用户输入)

作为一个函数:

String[] verify(String[] input, String[] dict)
{
    String[] output = new String[input.length];
    System.arraycopy(input, 0, output, 0, input.length);
    loop: for(int i = 0; i < output.length; i++)
        for(String s : dict)
            if((s.charAt(0) == output[i].charAt(0) ? 1 : 0) + (s.charAt(1) == output[i].charAt(1) ? 1 : 0) + (s.charAt(2) == output[i].charAt(2) ? 1 : 0) == 2)
            {
                output[i] = s;
                continue loop;
            }
    return output;
}

我确信有更好的方法来检查 3 位数字是否相同,if 语句只是总结有多少是相等的

要优先考虑相等的值(所以如果“cat”和“mat”是字典中的条目,用户输入的“cat”将不会更改为“mat”):

String[] verify(String[] input, String[] dict)
{
    String[] output = new String[input.length];
    System.arraycopy(input, 0, output, 0, input.length);
    loop: for(int i = 0; i < output.length; i++)
    {
        for(String s : dict)
            if(s.charAt(0) == output[i].charAt(0) && s.charAt(1) == output[i].charAt(1) && s.charAt(2) == output[i].charAt(2))
                continue loop;

        for(String s : dict)
            if((s.charAt(0) == output[i].charAt(0) ? 1 : 0) + (s.charAt(1) == output[i].charAt(1) ? 1 : 0) + (s.charAt(2) == output[i].charAt(2) ? 1 : 0) == 2)
            {
                output[i] = s;
                continue loop;
            }
    }

    return output;
}

要获得不同的匹配词、更正词和无效词数组:

String[][] verify(String[] input, String[] dict)
{
    List<String> matched = new ArrayList<String>();
    List<String> corrected = new ArrayList<String>();
    List<String> missing = new ArrayList<String>();

    loop: for(int i = 0; i < input.length; i++)
    {
        for(String s : dict)
            if(s.charAt(0) == input[i].charAt(0) && s.charAt(1) == input[i].charAt(1) && s.charAt(2) == input[i].charAt(2))
            {
                matched.add(input[i]);
                continue loop;
            }

        for(String s : dict)
            if((s.charAt(0) == input[i].charAt(0) ? 1 : 0) + (s.charAt(1) == input[i].charAt(1) ? 1 : 0) + (s.charAt(2) == input[i].charAt(2) ? 1 : 0) == 2)
            {
                corrected.add(s);
                continue loop;
            }
        missing.add(input[i]);
    }

    return new String[][]{matched.toArray(new String[0]), corrected.toArray(new String[0]), missing.toArray(new String[0])};
}
  • 输出[0] - 匹配的单词
  • 输出[1] - 更正的单词
  • 输出[2] - 缺少单词

【讨论】:

  • 这段代码似乎大部分都在工作,但我如何让它不添加根本不匹配的单词?例如,如果我刚刚输入“aaa”,它仍然会将它添加到我的最终字符串中
  • @kingabidalX 你真正想要的是什么?一组完全匹配和更正?每个数组?
  • 我只希望它有一个字符串,其中包含所有匹配的单词和一个字母更正的单词。如果有一个不正确的单词,则抛出 LengthException (我自己的异常),如果同一个单词的内容超出无法纠正的范围,则重新抛出相同的 LengthException (两个或更多不同的字母)这有意义吗?
  • @phflack-------
【解决方案2】:

试试这个我创建了一些假数据来测试

String[] userInput = {"cat", "dog", "run", "man"};

String[] fileInput = {"bat", "mat", "zzz", "aaa"};

ArrayList<String> sameInput = new ArrayList<>();

for (int i = 0; i < userInput.length; i++) {
    for (int j = 0; j < fileInput.length; j++) {
        char[] userChars = userInput[i].toCharArray();
        char[] fileChars = fileInput[j].toCharArray();

        boolean[] booleans = new boolean[userChars.length];


        for (int x = 0; x <= 2; x++)
            if (fileChars[x] == userChars[x])
                booleans[x] = true;
            else
                booleans[x] = false;

        int count = 0;
        for (boolean bool : booleans)
            if (bool)
                count++;

        if (count >= 2)
            sameInput.add(userInput[i]);
    }
}

for (String o : sameInput) System.out.println(o);

这有很多缺陷,因为有很多三个字母的单词有共同的字母但祝你好运

【讨论】:

  • 匹配词添加到哪里了?我正在尝试将匹配的单词添加到不同的字符串中。
  • 这段代码似乎根本没有改变字符串数组中的任何东西?你能再给我解释一下吗
  • if(a == b) bool = true; else bool = false; 可以简化为bool = a == b;。直接求和而不是使用 booleans 数组也可能更容易
  • @kingabidalX 也让我发现了一点,但它在fileInput 数组中将mat 更改为cat
  • @kingabidalX 它在我命名为 fileInput 的原始数组中对其进行了更改,如果可以提高效率,请随时编辑答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 2011-09-15
  • 2015-04-08
  • 1970-01-01
相关资源
最近更新 更多