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-19  โปรแกรมแปลคำอังกฤษเป็นไทย
import java.util.Scanner;
import jlab.URLStream;
// โปรแกรมแปลคำอังกฤษเป็นไทย 
public class English2Thai {
  public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    System.out.print("คำ = ");
    String eng = kb.nextLine();
    eng = eng.trim().toLowerCase();
    String url = "http://wordbook.rackhub.com/" + eng + ".html";
    Scanner web = new Scanner(new URLStream(url), "UTF-8");
    String thai = "";
    while (thai.equals("") && web.hasNext()) {
      String t = web.nextLine();
      //ต้องการหา <title>school [N] โรงเรียน   | wordbook...
      t = t.toLowerCase();
      int j = t.indexOf("<title>", 0);
      if (j >= 0) {
        j = t.indexOf("]", j+7);
        if (j >= 0) {
          int k = t.indexOf("|", j+1);
          if (k >= 0) thai = t.substring(j+1, k);
        }
      }
    }
    if (!thai.equals("")) {
      System.out.println("แปลว่า " + thai.trim());
    } else {
      System.out.println("ไม่พบคำแปล");
    }     
  }
}
©2009 S.Prasitjutrakul