This code is basically adding binary numbers. For some reason I keep getting an index out of bounds error on line
102 which is the nested for
loop if
statement towards the bottom. Can anyone guide me in the right direction?
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
}