0

I have written this code to generate the bit stream corresponding to string a and then invert specific bits in a bit stream.the variable cc gets an integer value in the processing before the code snippet.

However it gives Array Index Out Of Bounds Exception on the following line.

if(sb2.charAt(cc)=='1')

can anyone tell me what is the probable cause?

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());
    }
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
Manika
  • 29
  • 1
  • 10

1 Answers1

0

You haven't put anything inside sb2 variable before calling

if(sb2.charAt(cc)=='1')

and what ever the cc value, it exceeds the sb2.length() size.

Salah
  • 8,567
  • 3
  • 26
  • 43