此代碼基本上是添加二進制數字。由於某種原因,我不斷收到 行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
}
請發佈異常的堆棧跟蹤 –