java 统计字符串中指定字符出现的次数

下列程序:
import java.io.*;
public class Abc {
public static void main(String[] args) throws IOException {
byte str[]=new byte[20];
byte bt[]=new byte[2];
System.out.println("请输入一组字符...");
System.in.read(str) ;
System.out.println("请输入要统计的字符...");
System.in.read(bt);
int i;
int count=0;
for(i=0;i<=str.length;i++){
if(bt.equals(str[i]))
count++;
}
System.out.println("字符"+bt+"在字符串中出现的次数为:"+count);
}
}

请问:问题出在哪了?

第1个回答  2008-04-13
已经修改好,如下:
public static void main(String[] args) throws IOException{

byte str[]=new byte[20];
byte bt[]=new byte[2];
System.out.println("请输入一组字符...");
System.in.read(str) ;
System.out.println("请输入要统计的字符...");
System.in.read(bt);
int i;
int count=0;
System.out.println(str.length);
for(i=0;i<=str.length;i++){
if(str[i]!=0)
{
if(bt[0]==str[i])
{
count++;
System.out.println(str[i]);
}

}
else break;
}
System.out.println("字符"+bt+"在字符串中出现的次数为:"+count);
}
相似回答