我收到了 java.lang.StringIndexOutOfBoundsException
运行此代码时。我该如何解决这个问题?
import java.util.Scanner;
class C {
static int Occurence = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
StringBuilder sb = new StringBuilder();
System.out.println(sb.deleteCharAt(3));
for (int i = str.length() - 1; i > 0; i--) {
Occurence = 1;
for (int j = i - 1; j > 0; j--) {
if (str.charAt(i) == str.charAt(j)) {
Occurence++;
sb.deleteCharAt(j);
}
}
System.out.println(str.charAt(i) + " repeated " + Occurence);
}
}
}
最佳答案
// empty, current size 0
StringBuilder sb = new StringBuilder();
// StringIndexOutOfBoundException bcoz current size is 0, and you are accessing index 3.
System.out.println(sb.deleteCharAt(3));
关于java - 我面临 java.lang.StringIndexOutOfBoundsException :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202567/