Tự học guitar, lập trình, khiêu vũ, tennis, máy tính

Monday
Mar 15th
Text size
  • Increase font size
  • Default font size
  • Decrease font size

Home arrow Programming arrow Ngôn ngữ Java arrow JSP - Một ứng dụng từ điển Anh-Việt phần 11
JSP - Một ứng dụng từ điển Anh-Việt phần 11

JSP - Một ứng dụng từ điển Anh-Việt phần 11

Thật sự không phải do GetDemicalValue() mà do chúng ta còn khoảng trắng trong tham số truyền cho GetDemicalValue().

Và đây là code hoàn chỉnh. Bạn có thể chạy thử với các từ khác nhau.


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class TestJSP2 {

    public static double GetDemicalValue(String str) {
          String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
          double decValue = 0;
          double str_length = str.length();
            for(int i = 0; i<str_length;i++){
                char char_at = str.charAt(i);
                int pos_of_char = base64.indexOf(char_at);
                decValue = decValue + Math.pow(64,str_length-1-i)* pos_of_char;
            }
            return decValue;
     }
    public static String readFileAsString(String filePath)
    throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    
    }
    public static String readFileAsString2(String filePath)
    throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        List<String> Mylist = new ArrayList<String>();
      
        String contents="";
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            Mylist.add(readData);
            buf = new char[1024];
        }
        reader.close();
        String[] s1 = Mylist.toArray(new String[0]);
        for(int i = 0; i < s1.length; ++i){
            contents = s1[i];
        }
        return contents;
      
    }
    public static void main(String[] args) {

        try{
        String fIndexString = readFileAsString("C:\\anhviet109K.index");
        String fDictString = readFileAsString("C:\\anhviet109K.dict");
       
        int indexOfWord = fIndexString.indexOf("abacist");
        char as9 = (char)9;
        char as10 = (char)10;
        int beginOfPos = fIndexString.indexOf(as9,indexOfWord);
        int endOfPos = fIndexString.indexOf(as9,beginOfPos+1);
        int endOfCont = fIndexString.indexOf(as10,endOfPos+1);
      
      
        String posOfWordInDict = fIndexString.substring(beginOfPos, endOfPos);
        String contOfWordInDict = fIndexString.substring(endOfPos, endOfCont);
        System.out.println("Vi tri và do dai trong file INDEX: "+posOfWordInDict+"=="+contOfWordInDict);
       
        String pDec = posOfWordInDict.trim();
        String cDec = contOfWordInDict.trim();
        int posOfWordInDictDec = (int)GetDemicalValue(pDec);
        int contOfWordInDictDec = (int)GetDemicalValue(cDec);
        String contentInDict = fDictString.substring(posOfWordInDictDec, posOfWordInDictDec+contOfWordInDictDec);
        System.out.println("Noi dung trong file DICT: "+contentInDict);
      
        } catch (IOException e) {
              System.out.println("IOException: " + e);
            }
       
    }

}

Công việc còn lại chỉ là áp dụng vào JSP

 
-Lấy giá trị người sử dụng nhập vào textbox
-Truyền giá trị đó cho một biến
-Thay dòng int indexOfWord = fIndexString.indexOf("abacist"); bằng int indexOfWord = fIndexString.indexOf(variable); trong đó variable là biến nói trên
-Áp java code vào trang JSP khung mà chúng ta đã tạo trong phần trước.


 
< Trước   Tiếp >

Bookmark and Share