5. ข้อความ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 5-13  โปรแกรมนับจำนวนวรรณยุกต์ในพจนานุกรม
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
// โปรแกรมนับจำนวนวรรณยุกต์ในพจนานุกรม 
public class ToneCount {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new File("c:/java101/riwords.txt"));
    int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
    String t1 = "่";  // ไม้เอก 
    String t2 = "้";  // ไม้โท 
    String t3 = "๊";  // ไม้ตรี 
    String t4 = "๋";  // ไม้จัตวา 
    while (in.hasNext()) {
      String line = in.nextLine();
      line = line.trim();        
      if ( line.length()>0 && !"#".equals(line.substring(0,1)) ) {
        int k = -1;
        while (true) {  // นับจำนวนไม้เอกใน line
          k = line.indexOf(t1, k+1);
          if (k < 0) break;
          c1++;
        }
        k = -1;
        while (true) {  // นับจำนวนไม้โทใน line
          k = line.indexOf(t2, k+1);
          if (k < 0) break;
          c2++;
        }
        k = -1;
        while (true) {  // นับจำนวนไม้ตรีใน line
          k = line.indexOf(t3, k+1);
          if (k < 0) break;
          c3++;
        }
        k = -1;
        while (true) {  // นับจำนวนไม้จัตวาใน line
          k = line.indexOf(t4, k+1);
          if (k < 0) break;
          c4++;
        }
      }
    }
    System.out.println("ไม้เอกมี\t " + c1 + " ตัว");
    System.out.println("ไม้โทมี\t " + c2 + " ตัว");
    System.out.println("ไม้ตรีมี\t " + c3 + " ตัว");
    System.out.println("ไม้จัตวามี\t " + c4 + " ตัว");
  }
}
©2009 S.Prasitjutrakul