【发布时间】:2017-11-07 13:23:22
【问题描述】:
public static void main(String[] args) throws IOException {
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
System.out.println("Enter the string");
String s=br.readLine();
char[] ch=s.toCharArray();
findtherepeatechar(ch);
}
private static char findtherepeatechar(char[] ch) {
int i;
int count[]= {0};
for( i=0;i<ch.length;i++) {
count[ch[i]]++;
}
for(i=0;i<ch.length;i++)
if(count[ch[i]]>1) {
return ch[i];
}else {
return '\0';
}
return 0;
}
}
输入字符串
ABCD
线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常: 65 在 strs.findtherepeatechar(strs.java:18) 在 strs.main(strs.java:12)
提前致谢
【问题讨论】:
-
调试器会很快告诉你。那个计数数组显然是不对的。您似乎想要对每个字母进行计数,但这不是您在方法开始时分配的。
-
另外看看Java naming convention
IndexOutOfBoundsException表示你不在你的数组里了。 -
将 int count = {0} 更改为 int count[]= new int[1000];
标签: java indexoutofboundsexception