Java_CH10_HW2

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Java_CH10_HW2
  5. {
  6. public static void main(String[] args)
  7. {
  8. // TODO 自動產生的方法 Stub
  9. String filepath = "C:\\Users\\Dragonyue\\Desktop\\第 十章作業參考文件\\washington.txt";
  10. try
  11. {
  12. Scanner scan = new Scanner(System.in);
  13. InputStreamReader sr = new InputStreamReader(
  14. new FileInputStream(filepath));
  15. BufferedReader reader = new BufferedReader(sr);
  16. String str = "";
  17. int count = 0;
  18. int index = -1;
  19. int linenums = 0;
  20. System.out.print("Input character sequence:");
  21. String ch = scan.nextLine();
  22. while ((str = reader.readLine()) != null)
  23. {
  24. linenums++;
  25. index = str.indexOf(ch);
  26. if (index != -1)
  27. {
  28. System.out
  29. .println(String.format("i=%d %d", linenums, index));
  30. count++;
  31. }
  32. }
  33. System.out.println(String.format("字串%s總共次數%d", ch, count));
  34. reader.close();
  35. sr.close();
  36. }
  37. catch (IOException e)
  38. {
  39. // TODO 自動產生的 catch 區塊
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44.  

留言

這個網誌中的熱門文章

C# 井字遊戲