Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的程序中的逻辑是否接近解决方案?_Java_Algorithm_Debugging - Fatal编程技术网

Java 我的程序中的逻辑是否接近解决方案?

Java 我的程序中的逻辑是否接近解决方案?,java,algorithm,debugging,Java,Algorithm,Debugging,我试图计算一个字母在字符串中出现的次数(aabcccaaa),并将它出现的次数与相应的字母一起放入一个新字符串中。问题是我得到了一个StringIndexOutOfBoundsException 我有一点线索,但我认为主要是因为我的逻辑在这个问题上有缺陷 我走对了吗?我做错了什么?我该如何修复它 例如,输出应该是a2b1c5a3 这是我的密码: public class Problem { public static void main(String []args) { String

我试图计算一个字母在字符串中出现的次数(
aabcccaaa
),并将它出现的次数与相应的字母一起放入一个新字符串中。问题是我得到了一个
StringIndexOutOfBoundsException

我有一点线索,但我认为主要是因为我的逻辑在这个问题上有缺陷

我走对了吗?我做错了什么?我该如何修复它

例如,输出应该是
a2b1c5a3

这是我的密码:

public class Problem {

public static void main(String []args) {
    String str = "aabcccccaaa";
    System.out.println(compressBad(str));
}

public static String compressBad(String str) {
    int countConsecutive = 0;
    String compressedString = "";

    for(int i = 0; i < str.length(); i++) {
        if(str.charAt(i) != str.charAt(i + 1)) {
            countConsecutive++;
            compressedString += "" + str.charAt(i) + countConsecutive;
            countConsecutive = 0;
        }
    }
    return compressedString;
  }
}
公共类问题{
公共静态void main(字符串[]args){
String str=“aabcccaaa”;
System.out.println(compressBad(str));
}
公共静态字符串压缩错误(字符串str){
整数=0;
字符串压缩字符串=”;
对于(int i=0;i
i
是最后一个索引时,这一行
str.charAt(i+1)
将读取越界,
i+1
现在越界。

这一行
str.charAt(i+1)
将在
i
是最后一个索引时读取越界,
i+1
现在越界。

,下面是我要做的:

public static String compressBad(final String str) {

    if (str == null || str.length() < 0) {
        return "";
    }

    int countConsecutive = 0;

    StringBuilder sb = new StringBuilder();
    char previousLetter = str.charAt(0);

    for (char c : str.toCharArray()) {
        if (c == previousLetter) {
            countConsecutive++;
        } else {
            sb.append(previousLetter).append(countConsecutive);

            previousLetter = c;
            countConsecutive = 1;
        }
    }
    sb.append(previousLetter).append(countConsecutive);

    return sb.toString();
}
publicstaticstringcompressbad(最终字符串str){
如果(str==null | | str.length()<0){
返回“”;
}
整数=0;
StringBuilder sb=新的StringBuilder();
char-previousLetter=str.charAt(0);
for(字符c:str.toCharArray()){
if(c==上一个字母){
count++;
}否则{
附加(过去的字母)附加(连续的);
前一个字母=c;
连续计数=1;
}
}
附加(过去的字母)附加(连续的);
使某人返回字符串();
}

对于它的价值,以下是我要做的:

public static String compressBad(final String str) {

    if (str == null || str.length() < 0) {
        return "";
    }

    int countConsecutive = 0;

    StringBuilder sb = new StringBuilder();
    char previousLetter = str.charAt(0);

    for (char c : str.toCharArray()) {
        if (c == previousLetter) {
            countConsecutive++;
        } else {
            sb.append(previousLetter).append(countConsecutive);

            previousLetter = c;
            countConsecutive = 1;
        }
    }
    sb.append(previousLetter).append(countConsecutive);

    return sb.toString();
}
publicstaticstringcompressbad(最终字符串str){
如果(str==null | | str.length()<0){
返回“”;
}
整数=0;
StringBuilder sb=新的StringBuilder();
char-previousLetter=str.charAt(0);
for(字符c:str.toCharArray()){
if(c==上一个字母){
count++;
}否则{
附加(过去的字母)附加(连续的);
前一个字母=c;
连续计数=1;
}
}
附加(过去的字母)附加(连续的);
使某人返回字符串();
}

好的,我明白了。但就解决方案而言,我在
str.charAt(I+1)
之后的逻辑是否几乎正确?他解释了异常的原因,您的代码中确实有更多错误,调试程序以找出出现错误的原因。这是学习的唯一方法,明白了。但就解决方案而言,我在
str.charAt(I+1)
之后的逻辑是否几乎正确?他解释了异常的原因,您的代码中确实有更多错误,调试程序以找出出现错误的原因。这是学习cool的唯一方法,我欣赏这个答案:D。但我想用我的方法解决这个问题。酷,我欣赏这个答案:D。但我想用我的方法解决这个问题。