A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李兆宁 中级黑马   /  2012-10-2 14:11  /  2151 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李兆宁 于 2012-10-2 16:33 编辑
  1. class StrTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 String str = "abkkdfkkegegsdkkdsagfkk";
  6.                 int i = getSubCount2(str,"kk");
  7.                 sop("i="+i);
  8.         }

  9.         public static int getSubCount2(String str,String key)
  10.         {
  11.                 int count = 0;
  12.                 int index = 0;

  13.                 while ((index=str.indexOf(key,index))!=-1);
  14.                 {
  15.                         sop("index="+index);
  16.                         index = index + key.length();
  17.                         count++;
  18.                 }
  19.                 return count;
  20.         }

  21.         public static void sop(String str)
  22.         {
  23.                 System.out.println(str);
  24.         }
  25. }
复制代码
编译无错,运行正常,就是不出结果。。求教错误之处。。
上网不方便,设置解决可能会晚点。。

6 个回复

倒序浏览
目测 while ((index=str.indexOf(key,index))!=-1);
这一句多了个分号
回复 使用道具 举报
  1. public static int getSubCount(String str,String key)
  2.         {
  3.                 //key 指定要找的字符串
  4.                 int count = 0;   //计数器记录出现的次数
  5.                 int index = 0;   //每次出现指定字符串的索引

  6.                 while((index= str.indexOf(key,index))!=-1) // 循环找不到指定字符串,返回-1时终止循环
  7.                 {
  8.                         sop("index="+index);
  9.                         index = index + key.length();    //index+key.length,下次将会从出现指定字符串之后开始查找

  10.                         count++;
  11.                 }
  12.                 return count;
  13.         }

  14.         public static void main(String[] args)
  15.         {
  16.                 String str = "kkabkkcdkkefkks";

  17.                 System.out.printl("count="+getSubCount_2(str,"kk"));
  18.         }
复制代码
这个行的
回复 使用道具 举报
第15行  :多了一个分号!  所以不行,程序进入死循环了

我自己也写了个方法,分享下

public static void main(String[] args)
        {
                // TODO 自动生成的方法存根
                 String str = "abkkdfkkegegsdkkdsagfkk";
                 int index=getCount(str, "kk");
                 System.out.println(index);
        }
       
        public static int getCount(String str,String key)
        {
                int count=0;
                while(str.indexOf(key,0)!=-1)
                {
                        count++;
                        str=str.substring(str.indexOf(key,0)+key.length(), str.length());
                }
                return count;
        }
回复 使用道具 举报
做程序时一定要谨慎再谨慎啊{:soso_e127:}
回复 使用道具 举报
不知道为什么 你那代码卡死我了
  我就自己帮你写了个。 关键看你怎么用indexOF这个函数
public class test5 {

        /**
         * @param args
         */
       
        public static void main(String[] args) {
                String str = "kkajkdkklsjfkljskkfkk";
                int i = subString(str, "kk");
                System.out.println(i);
        }
        public static int subString(String str, String key)
        {
                int count = 0;
                int a  = str.indexOf(key, 0);
                while(a != -1)
                {
                        count++;
                        a = str.indexOf(key, a+2);  //每查一个 所言位置自加2个  为什么是2 ?  自家想
                }
                return count;
        }

}
回复 使用道具 举报
我了个去,为这个分号,,,感觉程序进入了死循环,怎么就没发现这个分号呢!
唉! 问题解决,谢谢各位。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马