2013-11-15 169 views
0

此代碼基本上是添加二進制數字。由於某種原因,我不斷收到 行102上的一個索引超出範圍的錯誤,這是嵌套的for循環if聲明向下。任何人都可以引導我在正確的方向嗎?線程「main」中的異常java.lang.ArrayIndexOutOfBoundsException:160

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

public class Process { 

    private String storeString="", x=""; 
    private String polynomial; 

    //get file 
    public void getFileName(String fileName) {       
    try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { 
     String sCurrentLine=""; 

     while ((sCurrentLine = br.readLine()) != null) { 
     storeString+= sCurrentLine.toUpperCase(); 
     } 
    } 
    catch (IOException e) { 
     System.out.println("Sorry no such file is was found!" + 
         "\n\nAre you sure that's the right file name"); 
    } 
    System.out.println(storeString); 

    if (storeString.matches("[0-9A-F]+") == true) { 
     System.out.println("true"); 
     hexToBin(storeString); 
    } else { 
     System.out.println("Sorry that is an Invalid Hex"); 
     System.exit(0); 
    }  
    System.out.println(x); 

    } 

    //convert to binary  
    public void hexToBin(String hex) { 
    String bin = ""; 
    String binFragment = ""; 
    int iHex; 
    hex = hex.trim(); 
    hex = hex.replaceFirst("0x", ""); 

    for (int i = 0; i < hex.length(); i++) { 
     iHex = Integer.parseInt(""+hex.charAt(i),16); 
     binFragment = Integer.toBinaryString(iHex); 

     while (binFragment.length() < 4) { 
     binFragment = "0" + binFragment; 
     } 
     bin += binFragment; 
    } 
    System.out.println("length:" + bin.length()); 
    Calculate(bin.length(), bin); 
    } 

    // calculations 
    public void Calculate(int size, String hex) { 
    char [] poly= new char[17]; 
    char [] charBin= new char[size]; 
    int move=-1,zero=0,L=0; 

    polynomial = "10000100110001101"; 

    //convert to char 
    for (int i=0; i < 17; i++) { 
     poly[i] = polynomial.charAt(i); 
    } 
    //convert hex to char 
    for (int i=0; i < size; i++) { 
     charBin[i] = hex.charAt(i); 
    } 

    //compare with if statements 
    for (int i=0; i < size ; i++) { 
     for(int j=0; j < 17; j++) { 
     move++; 
     if (charBin[move]=='1' && poly[j]=='1') { 
      charBin[move] = '0'; 
     } else if (charBin[move]=='0' && poly[j]=='0') { 
      charBin[move] = '0'; 
     } else { 
      charBin[move] = '1'; 
     } 
     } 

     int k=0; 
     //print charbin 
     //while (charBin[k]!= size) { 
     // System.out.print(charBin[k]); 
     // k++; 
     //} 

    }//end class 

    }//end class 

} 
+2

請發佈異常的堆棧跟蹤 –

回答

3

既然你循環i高達sizej高達每次17和增量move,你可以期望move起牀17 * size - 1。您正在使用它來索引一個長度等於size的數組。

char[] charBin = new char[size]; 
int move = -1; 
... 
for(int i=0;i < size ; i++) { 
    for(int j=0;j < 17;j++) { 
     move++; 
     if(charBin[move]) ... 
    } 
} 
+0

太棒了所以如果我重置移動我不應該得到這個錯誤? –

+0

謝謝大家的幫助 –

2

charBinsize位置。但是您使用move作爲索引訪問循環中的值。從0i運行

char[] charBin = new char[size]; 
int move = -1; 
for(int i=0;i < size ; i++) { 
    ... 
    for(int j=0;j < 17;j++) { 
     ... 
     move++; 
    } 
} 

在外環到size並在內環j運行從017move正在遞增每次您的第二循環內。因此move的潛在最大值爲size * 17。只要movesize的值相同,您將會遇到異常,因爲您處於數組邊界之外(charBin中的有效位置從0運行到size - 1)。

相關問題