Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 160 [duplicate]
-
20-12-2019 - |
Domanda
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
}
Soluzione
Since you loop i
up to size
and j
up to 17 and increment move
each time, you can expect move
to get up to 17 * size - 1
. You are using it to index into an array of length equal to 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]) ...
}
}
Altri suggerimenti
charBin
has size
locations. But you are accessing its values in your loop using move
as an index. move
is being incremented every time inside your second loop:
char[] charBin = new char[size];
int move = -1;
for(int i=0;i < size ; i++) {
...
for(int j=0;j < 17;j++) {
...
move++;
}
}
In the outer loop i
runs from 0
to size
and in the inner loop j
runs from 0
to 17
. move
therefore has a potential maximum value of size * 17
. As soon as move
becomes the same value as size
, you will get an exception since you are outside the bounds of the array (valid locations in charBin
run from 0
to size - 1
).