package com.guoxiaoming.string; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; //处理多个字符串输入,每个输入占据一行 //统计每一行当中的u字符的个数 public class CountCharacter { public static void main(String[] args) { new CountCharacter().getResult(); } private void getResult() { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream( "src/com/guoxiaoming/string/test.txt"))); String str = null; int count = 0; while ((str = br.readLine()) != null) { for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'u') { count++; } } System.out.println("The count of 'u' is " + count); count = 0; str = null; } } catch (FileNotFoundException ex) { System.out.println("file not found!"); } catch (IOException ex) { ex.printStackTrace(); } } }
The count of 'u' is 11
The count of 'u' is 1