簡體   English   中英

為什么我的 Java 代碼 output 10 而不是 -1 在這種情況下,我該如何解決?

[英]Why does my Java code output 10 instead of -1 in this situation, and how do I fix it?

我正在創建一個 static 方法“indexOfKeyword”,它旨在在字符串未嵌入到另一個詞中時返回字符串的 indexOf——它意味着在沒有出現這種情況時返回 -1。

鑒於

String s = "She sells seashells by the seashore.";
String keyword = "sea";

output 應該是 -1,因為關鍵字“sea”嵌入到每個單詞中; 然而,我的代碼輸出 10,這是它在“貝殼”中找到“海”的第一次出現。

如果一個字符串在開頭單獨出現,比如

String s = "Carolyn has a car that is scary fast.";
String keyword = "car";

我在startIdx必須大於 0 的地方做了它,這樣“Carolyn”中的“Car”就不會被拾取。 當上面的代碼輸入到下面的代碼中時,它按預期工作,正確輸出 14。

這是完整的代碼,逐字記錄,應該輸出 -1:

public class Chatter {
    public static int indexOfKeyword(String s, String keyword) {
        s = s.toLowerCase();
        keyword = keyword.toLowerCase();
        int startIdx = s.indexOf(keyword);
        while (startIdx >= 0) {
            String before = " ", after = " ";
            if (startIdx > 0) {
                before = s.substring(startIdx - 1, startIdx);
            }
            int endIdx = startIdx;
            if (endIdx < s.length()) {
                after = s.substring((startIdx + keyword.length()), (startIdx + keyword.length() + 1));
            }
            if (!(before.compareTo("a") >= 0 && before.compareTo("z") <= 0 && after.compareTo("a") >= 0
                    && after.compareTo("z") <= 0)) {
                if (startIdx > 0) {
                    return startIdx;
                }
            }
            startIdx = s.indexOf(keyword, s.indexOf(keyword) + 1);
        }
        return -1;
    }

    public static void main(String[] args) {
        // ... and test it here
        String s = "She sells seashells by the seashore.";
        String keyword = "sea";
        System.out.println(indexOfKeyword(s, keyword));
    }
}

我知道您想查找特定的詞而不是特定的 substring。

您的方法indexOfKeyword中有兩個錯誤。

  1. while循環中的條件是錯誤的。 您需要將其分成兩個獨立的條件。
  2. 設置startIdx以搜索下一次出現的keyword也是錯誤的。

將您的代碼與下面的代碼進行比較。

public static int indexOfKeyword(String s, String keyword) {
    int startIdx = s.indexOf(keyword);
    while (startIdx >= 0) {
        String before = " ", after = " ";
        if (startIdx > 0) {
            before = s.substring(startIdx - 1, startIdx);
        }
        int endIdx = startIdx;
        if (endIdx < s.length()) {
            after = s.substring((startIdx + keyword.length()), (startIdx + keyword.length() + 1));
        }
        if (!(before.compareTo("a") >= 0 && before.compareTo("z") <= 0)) {
            if (!(after.compareTo("a") >= 0 && after.compareTo("z") <= 0)) {
                if (startIdx > 0) {
                    return startIdx;
                }
            }
        }
        startIdx = s.indexOf(keyword, startIdx + 1);
    }
    return -1;
}

正如@Tom 提到的,在他的評論中,還有其他方法可以解決這個問題。 我假設您的目的是想出並實施您自己的算法,因此我向您展示了您在實施過程中哪里出了問題。

請注意,單個空格不是分隔句子中單詞的唯一方法,例如,句子中的兩個單詞可以用逗號分隔。

對不起,也許我理解錯了你的意圖,但你有沒有考慮過在搜索之前用空格包裹你的鑰匙?

這樣的事情應該(理論上)有效:

public static int indexOfKeyword(String s, String keyword) {
    String source = s.toLowerCase();
    String key = " " + keyword.toLowerCase() + " ";
    return source.indexOf(key);
}

或者(正如@Tom所指出的那樣)可能會使用 RegEx,但是此解決方案更復雜並且可能不像您希望的那樣明顯。

在您的情況下,它可能看起來像這樣:

public static int indexOfKeyword(String s, String keyword) {
    Matcher m = Pattern.compile("\\s" + keyword + "\\s", Pattern.CASE_INSENSITIVE).matcher(s);
    return m.find() ? m.start() : -1;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM