【问题标题】:Exception in program for search anagrams in 2 strings [duplicate]在 2 个字符串中搜索字谜的程序中出现异常 [重复]
【发布时间】:2015-08-29 11:13:26
【问题描述】:

我对这个 Java 中的小程序有疑问,它检查 2 个字符串是否是字谜。

我收到StringIndexOutOfBoundsException

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
    at java.lang.String.charAt(Unknown Source)
    at AreAnagrams.areAnagrams(AreAnagrams.java:9)
    at AreAnagrams.main(AreAnagrams.java:30)

这是我的代码:

public class AreAnagrams {
    public static boolean areAnagrams(String a, String b) {
        int j = 0;
        int i = 0;
        if (a.length() == b.length()) {
            while (i < a.length()) {
                if (a.charAt(i) == b.charAt(j)) {
                    j++;
                    i = 0;
                } else {
                    i++;
                    if (j > a.length()) {
                        return false;
                    }
                }
            }
        } else {
            return false;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(areAnagrams("momdad", "dadmom"));
    }
}

【问题讨论】:

  • 看看第 9 行。去掉 i = 0。可能还有更多我没有发现的错误。

标签: java string exception


【解决方案1】:

java.lang.StringIndexOutOfBoundsException 在您引用超过字符串长度的字符索引时发生。

例如字符串 "dadmom" - 当你调用 charAt(6) 时,它会抛出这个异常,因为字符索引在 0 到 5。

您可以使用以下代码来识别字谜:

public static boolean areAnagrams(String a, String b) {
     char[] aChars = a.replaceAll("\\s", "").toCharArray();
     char[] bChars = b.replaceAll("\\s", "").toCharArray();
     Arrays.sort(aChars);
     Arrays.sort(bChars);
     System.out.println(aChars);
     System.out.println(bChars);

     return Arrays.equals(aChars, bChars);
}

public static void main(String[] args) {
     System.out.println(areAnagrams("momdad", "dadmom"));
}

【讨论】:

  • 我该如何解决?我尝试使用 a.charAt(i-1) 但我有相同的结果
  • @v3ctor 请检查 Erandas 更新他/她的答案。另外请注意,如果您喜欢使用不区分大小写的检查(例如,MomDaddadmom 的变位词),请在 a 和 @987654326 上另外调用 toLowerCase @.
  • 排序的计算成本很高,很容易解决,但我认为不是最好的
【解决方案2】:

我觉得你这里有一个编程逻辑错误。对于字谜,标准应该是第一个字符串从左到右开始,第二个字符串从右到左开始的字符应该相等。

我在您的代码中没有找到任何此类内容。我觉得您应该在 if 块 if ( a.length() == b.length()) 中尝试以下操作:

int length = a.length();
for(int i = 0; i < length; i++){
    if(a.charAt(i) != b.charAt(length-i-1)){
        return false;
    }
}
return true;

您还应该删除代码中ij 变量的声明。

更正

我真的对字谜和回文感到困惑。上述答案对于回文是正确的。我正在为 anagram 添加我的答案。

我建议您通过更改方法areAnagrams 来递归检查字符串,如下所示:

public static boolean areAnagrams(String a, String b) {

//If the length of strings is unequal then return false.
    if(a.length() != b.length()){
        return false;
    } 

//Else if the length of strings equals 1 return the equality of the two strings
    if(a.length() == 1){
        return a.equals(b);
    }

//Else replace the first occurrence of the first character 
//of variable `a` with blank string in string variable b. 
//Here if the character is not present in string b then the
//variable b remains unchanged and the length of b would be 
//greater than that of variable `a` in the next recursion.
    b = b.replaceFirst(a.substring(0, 1), "");

//remove the first character in string `a`
    a = a.substring(1, a.length());

//make the recursive call
    return areAnagrams(a, b);
} 

【讨论】:

  • yes 是可以的,但如果我有 "jack" 而 "ckja" 是假的
  • @v3ctor jack 的变位词是kcaj 而不是ckja,因此它将返回false。所以你在评论中所说的实际上是一个正确的输出。
  • 字谜是一种文字游戏,将单词或短语的字母重新排列以产生新单词或短语的结果,使用所有原始字母恰好一次,然后我可以与字符串“jack”的所有 4 个字符
  • @Blip 你的意思是palindrome
  • @v3ctor 感谢您纠正我。我现在已经更正了我的答案
猜你喜欢
  • 2013-01-18
  • 2012-10-11
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
相关资源
最近更新 更多