2014-10-18 101 views
3

我有一个样品的HTML这是我从PracticalCryptography.com了,帮我计算随机文本的一致指数需要帮助JS移植到Java

<html> 
 

 
<body> 
 
    <script type="text/javascript"> 
 
    function GetIC() { 
 
     plaintext = document.getElementById("p").value.toLowerCase().replace(/[^a-z]/g, ""); 
 
     var counts = new Array(26); 
 
     var totcount = 0; 
 
     for (i = 0; i < 26; i++) counts[i] = 0; 
 
     for (i = 0; i < plaintext.length; i++) { 
 
     counts[plaintext.charCodeAt(i) - 97] ++; 
 
     totcount++; 
 
     } 
 
     var sum = 0; 
 
     for (i = 0; i < 26; i++) sum = sum + counts[i] * (counts[i] - 1); 
 
     ic = sum/(totcount * (totcount - 1)); 
 
     document.getElementById("ic").value = ic; 
 
     document.getElementById("count").value = totcount; 
 
    } 
 
    </script> 
 
    <form> 
 
    <p> 
 
     <textarea name="p" id="p" rows="2" cols="50" wrap="soft">Defend the east wall of the castle</textarea> 
 
    </p> 
 
    <p> 
 
     <input name="b" id="b" value="Get I.C." onclick="GetIC()" type="button"> 
 
    </p> 
 
    <p>Index of Coincidence = 
 
     <input id="ic" name="ic" size="15" maxchars="15" value="" type="text">Character Count = 
 
     <input id="count" name="count" size="8" maxchars="8" value="" type="text"> 
 
    </p> 
 
    </form> 
 
</body> 
 

 
</html>

我想将JavaScript代码移植到Java。下面是我的尝试:

private static boolean TestIOC(String text) { 
    // Replace any character *not* in the range a-z 
    // /g  -- global tag means find all, not just find one 
    String plaintext = text.toLowerCase().replaceAll("/[^a-z]/g", ""); 
    int counts[] = new int[26]; 
    int totcount = 0; 

    for (int i = 0; i < 26; i++) counts[i] = 0; 
    for (int i = 0; i < plaintext.length(); i++) { 
     int codePointAtI = Character.codePointAt(plaintext, i); 
     //System.out.println(codePointAtI); 

     counts[codePointAtI - 97]++; // Problematic line 
     totcount++; 
    } 

    int sum = 0; 
    float ic = 0; 

    for (int i = 0; i < 26; i++) { 
     sum = sum + counts[i]*(counts[i]-1); 
     ic = sum/(totcount*(totcount-1)); 
    } 

    if (ic >= 0.062 || ic <= 0.072) { 
     DecimalFormat df = new DecimalFormat("#.###"); 
     System.out.println(df.format(ic)); 
     return true; 
    } 

    else { 
     return false; 
    } 
} 

但是它有一个

java.lang.ArrayIdexOutOfBoundException

counts[codePointAtI - 97]++;

输出的明文

捍卫城堡的东墙

注释行的输出:System.out.println(codePointAtI);

+0

什么是在刚刚错误之前的注释行输出? – 2014-10-18 01:38:46

+2

尝试打印“明文”变量的值 - Java中的正则表达式与JavaScript中的正则表达式无法正常工作。请参阅http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html – dnault 2014-10-18 01:40:27

+0

我认为@dnault有它。链接上的示例不包含正斜杠。 – 2014-10-18 01:42:00

回答

1
private static boolean TestIOC(String text) { 
    // Replace any character *not* in the range a-z, and change all characters to lowercase. 
    String plaintext = text.toLowerCase().replaceAll("[^a-z]", ""); 

    //System.out.println(plaintext); 

    int counts[] = new int[26]; 
    int totcount = 0; 
    double sum = 0; 
    double ic = 0; 

    for (int i = 0; i < 26; i++) counts[i] = 0; 
    for (int i = 0; i < plaintext.length(); i++) { 
     int codePointAtI = Character.codePointAt(plaintext, i); 
     //System.out.println(codePointAtI); 

     counts[codePointAtI - 97]++; 
     totcount++; 
    } 

    //System.out.println("Totcount: " + totcount); 

    for (int i = 0; i < 26; i++) { 
     sum = sum+counts[i]*(counts[i]-1);   
     ic = sum/(totcount*(totcount-1)); 
    } 

    //System.out.println("Sum:" + sum); 
    //System.out.println("ic: " + ic); 

    if (ic >= 0.059 && ic <= 0.068) { 
     System.out.println("This file contains an English text."); 
     return true; 
    } 

    else { 
     System.out.println("This file does not contain an English text."); 
     return false; 
    } 
}