标签:count() ges blog imp dex main with size block
1)在《西游记》这本小说中,读取内容,统计某个词(任意词)出现的次数。
2)统计这本书一共有多少章(回)
3)统计这本书中一共有多少句子(带标点就算一句)
1 package 课后练习; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 9 /** 10 * 功能: 11 * 1)在《西游记》这本小说中,读取内容,统计某个词(任意词)出现的次数。 12 * 2)统计这本书一共有多少章(回) 13 * 3)统计这本书中一共有多少句子(带标点就算一句) 14 * @author Administrator 15 */ 16 public class XiYouJi { 17 18 19 /**功能: 20 * @param args 21 */ 22 public static void main(String[] args) { 23 24 int countWord = getWordCount("三藏"); 25 System.out.println("三藏出现的次数:"+countWord); 26 27 int countChapter = getChapter(); 28 System.out.println("西游记总共有:"+countChapter+"个章节"); 29 30 int countSentance = getSentanceCount(); 31 System.out.println("西游记总共有:"+countSentance+"个句子"); 32 } 33 34 /** 35 * 功能: 3)统计这本书中一共有多少句子(带标点就算一句) 36 * @return 37 */ 38 private static int getSentanceCount() { 39 BufferedReader br = null; 40 int num = 0; 41 try { 42 br = new BufferedReader(new FileReader(new File("E:/JavaSECode/7月/Me/day16/src/课后练习/西游记.txt"))); 43 String readLine =null; 44 while ((readLine = br.readLine()) != null) { 45 num += readLine.split("。").length-1; 46 if (readLine.contains("?")) { 47 num++; 48 } 49 if (readLine.contains("!")) { 50 num++; 51 } 52 } 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 return num; 57 } 58 59 /** 60 * 功能: 统计这本书一共有多少章(回) 61 * @return 62 */ 63 private static int getChapter() { 64 BufferedReader br = null; 65 int num = 0; 66 try { 67 br = new BufferedReader(new FileReader(new File("E:/JavaSECode/7月/Me/day16/src/课后练习/西游记.txt"))); 68 String readLine = null; 69 while ((readLine = br.readLine()) != null) { 70 if (readLine.startsWith("第") && readLine.endsWith("回") && (readLine.length() < 6)) { 71 num++; 72 } 73 } 74 } catch (IOException e) { 75 e.printStackTrace(); 76 } 77 return num; 78 } 79 80 /** 81 * 功能: 1)在《西游记》这本小说中,读取内容,统计某个词(任意词)出现的次数。 82 * @param word 83 * @return 84 */ 85 private static int getWordCount(String word) { 86 int count = 0; 87 BufferedReader br = null; 88 try { 89 br = new BufferedReader(new FileReader(new File("E:/JavaSECode/7月/Me/day16/src/课后练习/西游记.txt"))); 90 String str = null; 91 while ((str = br.readLine()) != null) { 92 count += str.split(word).length-1; 93 if (str.endsWith(word)) { 94 count++; 95 } 96 } 97 } catch (IOException e) { 98 e.printStackTrace(); 99 } finally { 100 if (br != null) { 101 try { 102 br.close(); 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 } 107 } 108 return count; 109 } 110 111 }
标签:count() ges blog imp dex main with size block
原文地址:http://www.cnblogs.com/snow1234/p/7204882.html