我在运行此代码时收到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);
}
}
}
答案 0 :(得分:0)
// 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));