我编写了这段代码来生成对应于字符串a的比特流,然后反转 位流中的特定位。变量cc在代码片段之前的处理中获得整数值。
但是它在下一行中给出了Array Index Out Of Bounds Exception。
if(sb2.charAt(cc)=='1')
谁能告诉我可能的原因是什么?
package abc1;
import java.util.Scanner;
public class abc2 {
public static void main(String s[]) {
String seckey = " ";
String cbs = " ";
String a = "Manikaparasher";
System.out.println("String originally:" + a);
char[] car = a.toCharArray();
StringBuilder sb = new StringBuilder();
for (char c : car) {
cbs = Integer.toBinaryString((int) c);
sb.append(cbs);
}
Scanner sc = new Scanner(System.in);
System.out.println(" Enter your passkey ");
int n = sc.nextInt();
int ul, ll;
double nv1, nv2, ns;
ll = n * 3;
ul = n * 7;
nv1 = ll + (Math.random() * (ul - ll));
nv2 = ll + (Math.random() * (ul - ll));
int nv11 = (int) nv1 % 7; //inversion bit
int nv22 = (int) nv2 % 3; //embedding bit
ns = ll + (Math.random() * (ul - ll));
int ns1 = (int) ns % 5;//start bit
if (nv11 == nv22) {
nv11++;
}
if (nv22 == ns) {
ns++;
}
if (nv11 == ns) {
nv11++;
}
seckey = "I" + nv11 + "E" + nv22 + "S" + ns1;
System.out.println(seckey);
System.out.println("old" + sb.toString());
System.out.println("nv11" + nv11);
StringBuilder sb2 = new StringBuilder(sb);
int cc = 0;
while (!cbs.isEmpty()) {
cc = cc + nv11;
if (sb2.charAt(cc) == '1') {
sb2.setCharAt(cc, '0');
sb2.append(sb);
} else {
sb2.setCharAt(cc, '1');
sb2.append(sb);
}
}
System.out.println("new" + sb2.toString());
}
}
答案 0 :(得分:0)
在调用
之前,您没有在sb2
变量中放置任何内容
if(sb2.charAt(cc)=='1')
以及cc
值,它超过sb2.length()
大小。