在尝试检测字符串是否嵌入另一个字符串时,发出无限循环问题

时间:2015-10-22 01:08:37

标签: java string while-loop infinite-loop

对于一个类项目,我需要修复一些代码才能使其正常运行,目标是创建一个带两个输入的方法,即两个字符串。如果'keyword'嵌入's'中的另一个单词或不存在,则返回-1,否则返回's'中第一个'keyword'实例的索引,该索引未嵌入另一个单词中。

但是,每次运行代码时,我都会遇到无限循环错误。我意识到删除'!'第30行允许代码运行,但每次出现都会输出完全错误的答案。有谁知道为什么我得到这个无限循环错误,或者如何使这个代码功能。谢谢!

public static int indexOfKeyword(String s, String keyword) {
// Change both s and keyword to lower case
s = s.toLowerCase();
keyword = keyword.toLowerCase();

// The index of the first occurrence (perhaps embedded) of keyword in s
int startIdx = s.indexOf(keyword);

// Check if this occurrence is embedded and look further down s if it is
while (startIdx >= 0) {
    // Find the substrings of length 1 immediately before and after
    // this occurrence. Default to the string " " containing only a space.
    String before = " ", after = " ";

    if (startIdx > 0) {
        before = s.substring(s.indexOf(keyword));
    }

    int endIdx = s.indexOf(keyword) + keyword.length();

    if (endIdx < s.length()) {
        after = s.substring(endIdx);
    }

    // If before and after aren't letters, this is the first whole word occurrence
    if (!((before.compareTo("a") >= 0 && before.compareTo("z") <= 0) &&
            (after.compareTo("a") >= 0 && after.compareTo("z") <= 0.))) {
        return startIdx;
    }

    // This is not a whole word occurrence. Move to the next occurrence.
    startIdx = s.indexOf(keyword, endIdx);
}

return -1;
}

3 个答案:

答案 0 :(得分:0)

您找到beforeafter的方式非常时髦,您是不是应该在关键字前后找到单个字符?因为现在你要设置多个字符作为之前和之后。这样的事情应该有效。

before = s.charAt(s.indexOf(keyword)-1);
after = s.charAt(endIdx)+1;

然后检查beforeafter是否为空格

if(before == ' ' && after == ' '){
    //the keyword is not embedded
}

答案 1 :(得分:0)

如果我没有弄错,之前应该在发生之前找到该字符,但是你从发生地点的索引中找到了子字符串?

另外我相信你的endIdx变量有1个错误,应该是startIdx + keyword.length - 1

答案 2 :(得分:0)

首先,before应为:

        before = s.substring(startIdx - 1, startIdx);

if应该是:

        // If before and after aren't letters, this is the first whole word occurrence
        if ((!(before.compareTo("a") >= 0 && before.compareTo("z") <= 0) && !(after.compareTo("a") >= 0 && after.compareTo("z") <= 0))) {
            return startIdx;
        }

完整代码:

public static int indexOfKeyword(String s, String keyword) {
    // Change both s and keyword to lower case
    s = s.toLowerCase();
    keyword = keyword.toLowerCase();

    // The index of the first occurrence (perhaps embedded) of keyword in s
    int startIdx = s.indexOf(keyword);

    // Check if this occurrence is embedded and look further down s if it is
    while (startIdx >= 0) {
        // Find the substrings of length 1 immediately before and after
        // this occurrence. Default to the string " " containing only a space.
        String before = " ", after = " ";

        if (startIdx > 0) {
            before = s.substring(startIdx - 1, startIdx);
        }

        int endIdx = s.indexOf(keyword) + keyword.length();

        if (endIdx < s.length()) {
            after = s.substring(endIdx);
        }

        // If before and after aren't letters, this is the first whole word occurrence
        if ((!(before.compareTo("a") >= 0 && before.compareTo("z") <= 0) && !(after.compareTo("a") >= 0 && after
                .compareTo("z") <= 0))) {
            return startIdx;
        }

        // This is not a whole word occurrence. Move to the next occurrence.
        startIdx = s.indexOf(keyword, endIdx);
    }

    return -1;
}