简体   繁体   English

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:45

[英]java.lang.StringIndexOutOfBoundsException: String index out of range: 45

I'm trying to make a program that takes in a string like: KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG我正在尝试制作一个接受以下字符串的程序: KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG

And returns something like this: $K13BCC$D15$K5MNUUU$G5并返回如下内容: $K13BCC$D15$K5MNUUU$G5

Another example is XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK另一个例子是XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK

Returns: XYZ*A6GGT*C6TTT*A14KK返回: XYZ*A6GGT*C6TTT*A14KK

But i get this StringIndexOutOfBoundsException when i try the first input, can anyone tell me why?但是当我尝试第一个输入时,我得到了这个 StringIndexOutOfBoundsException,谁能告诉我为什么? Here's my code:这是我的代码:

import java.util.Scanner;

class RunLengthEncoding {
    public static void main(String[] args) {
        Scanner h = new Scanner(System.in);
        String s;
        char g;
        System.out.print("Enter input string: ");
        s = h.next();

        for (int d = 0; d < s.length(); d++){
            if (!Character.isUpperCase(s.charAt(d))){
                System.out.print("Bad input.");
                return;
            }
        }

        System.out.print("Enter flag character: ");
        g = h.next().charAt(0);

        if (g != '#' && g != '$' && g != '&' && g != '*'){
            System.out.println("Bad input.");
            return;
        }

        char c = s.charAt(0);
        String encode = "";

        for (int n = 0; n < s.length() - 1; n++){
            int k = 0;
            int j = 0;

            while (k + n < s.length() && s.charAt(k + n) == c){
                j++;
                k++;
            }

            if (j > 3){
                encode += g;
                encode += c;
                encode += j;
                n += j - 1;
            }

            else {
                encode += c;
            }
            c = s.charAt(n + 1);
        }
        System.out.println("Encoded: " + encode);
    }
}

The reason that you are getting an out of bounds exception is because you are incrementing n outside of the for loop statement.您获得越界异常的原因是因为您在 for 循环语句之外递增n You do this when you are doing n += j - 1;当你在做n += j - 1;时,你会这样做。 . . This gives you an out of bounds exception because when you do c = s.charAt(n + 1);这会给您一个超出范围的异常,因为当您执行c = s.charAt(n + 1); , n could be greater than or equal to the length of the string. , n 可以大于或等于字符串的长度。 As a general rule, you should not alter the value of the iteration variable in the for loop anywhere outside of the for loop.作为一般规则,您不应在 for 循环之外的任何地方更改 for 循环中迭代变量的值。 It makes the code harder to debug.它使代码更难调试。

For anyone interested in the solution I made:对于任何对我提出的解决方案感兴趣的人:

import java.util.Scanner;

public class RunLengthEncoding {
    public static void main(String[] args){
        Scanner h = new Scanner(System.in);
        String s;
        char g;
        StringBuilder encode = new StringBuilder();

        System.out.print("Enter input string: ");
        s = h.next();

        for (int d = 0; d < s.length(); d++) {
            if (!Character.isUpperCase(s.charAt(d))) {
                System.out.print("Bad input.");
                return;
            }
        }

        System.out.print("Enter flag character: ");
        g = h.next().charAt(0);

        if (g != '#' && g != '$' && g != '&' && g != '*') {
            System.out.println("Bad input.");
            return;
        }

        for (int n = 0; n < s.length(); n++) {
            int k = 1;

            while (n < s.length() - 1 && s.charAt(n) == s.charAt(n + 1)) {
                k++;
                n++;
            }
            if (k > 3) {
                encode.append(g).append(s.charAt(n)).append(k);
            }

            else {
                for (int c = 0; c < k; c++) {
                    encode.append(s.charAt(n));
                }
            }
        }
        System.out.print("Encoded: " + encode);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java错误java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - Java Error java.lang.StringIndexOutOfBoundsException: String index out of range: 0 “ main” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:17 - “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 17 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: - java.lang.StringIndexOutOfBoundsException: String index out of range: java.lang.StringIndexOutOfBoundsException:yuicompressor中的字符串索引超出范围 - java.lang.StringIndexOutOfBoundsException: String index out of range in yuicompressor java.lang.StringIndexOutOfBoundsException:字符串索引超出范围 - java.lang.StringIndexOutOfBoundsException: String index out of range java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1 - java.lang.StringIndexOutOfBoundsException: String index out of range: 1 “异常:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围” - “Exception:java.lang.StringIndexOutOfBoundsException: String index out of range” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 - java.lang.StringIndexOutOfBoundsException: String index out of range: 4 Java.Lang.Stringindexoutofboundsexception索引超出范围(0) - Java.Lang.Stringindexoutofboundsexception index out of range (0) java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:3 - java.lang.StringIndexOutOfBoundsException: String index out of range: 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM