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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 814506914 中级黑马   /  2016-6-1 22:02  /  797 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

现有一串字符串 上海传智播客,上海黑马,武汉传智播客,深圳黑马,北京传智播客,广州黑马,北京黑马 ,要求使用程序统计出传智播客黑马在该字符串中出现的次数,然后按照指定格式输出到当前项目下的times.txt

2 个回复

倒序浏览
你是没有思路呢还是这部分内容还没学呢?
回复 使用道具 举报
本帖最后由 18611643318 于 2016-6-1 23:01 编辑
  1. package cn.itcast.test;

  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;

  5. /**
  6. * 现有一串字符串 ”上海传智播客,上海黑马,武汉传智播客,深圳黑马,北京传智播客,广州黑马,北京黑马” ,
  7. * 要求使用程序统计出”传智播客”和”黑马”在该字符串中出现的次数,然后按照指定格式输出到当前项目下的times.txt中
  8. *
  9. *
  10. * @author Venus
  11. *
  12. */
  13. public class Test2 {
  14.         public static void main(String[] args) {
  15.                 //字符串,定义大串,小串
  16.                 String str = "上海传智播客,上海黑马,武汉传智播客,深圳黑马,北京传智播客,广州黑马,北京黑马";
  17.                 String czbk = "传智播客";
  18.                 String hm = "黑马";
  19.                 //调用方法传参czbk
  20.                 int i = method(str, czbk);
  21.                 //调用方法传参hm
  22.                 int j = method(str, hm);
  23.                 String czbks = czbk+"\t在"+str+"中出现\t"+i+"次";
  24.                 String hms = hm+"\t在"+str+"中出现\t"+j+"次";
  25.                 System.out.println(czbks);
  26.                 System.out.println(hms);
  27.                 String finalstr = czbks+"\r\n"+hms+"\r\n";
  28.                 //调用write方法传参finalstr,写入文件
  29.                 write(finalstr);
  30.         }
  31.         public static int method(String str,String small){
  32.                 //定义计数器
  33.                 int count = 0;
  34.                 //索引初始值
  35.                 int index = 0;
  36.                 //index 等于 返回指定子字符串在此字符串中第一次出现处的索引。没有则返回-1
  37.                 while ((index = str.indexOf(small)) != -1) {       
  38.                         count++;
  39.                         str = str.substring(index+small.length());
  40.                 }
  41.                 return count;
  42.         }
  43.         public static void write(String s){
  44.                 File file = new File("times.txt");
  45.                 FileOutputStream fos =null;
  46.                 try {
  47.                          fos = new FileOutputStream(file,true);
  48.                         fos.write(s.getBytes());
  49.                 } catch (Exception e) {
  50.                         // TODO Auto-generated catch block
  51.                         e.printStackTrace();
  52.                 }finally {
  53.                         if(fos != null){
  54.                         try {
  55.                                 fos.close();         //关闭流
  56.                         } catch (IOException e) {
  57.                                 // TODO Auto-generated catch block
  58.                                 e.printStackTrace();
  59.                         }
  60.                 }
  61.                 }

  62.         }
  63. }
复制代码

写得不好,凑活看吧.在java基础day12中,有在大串中找小串的视频及详细讲解.
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马