I need to encrypt a text message in a two dimensional array(closest square matrix to number of letters in the text message) putting each letter that comes first in the first row, then second row... etc
It is not working and I don't understand the error messages.
public class CaesarBox { public static void main(String[] args) { // CaesarsBox <-encrypt|-decrypt> if(args[0].equals("-encrypt")){ System.out.println(encrypt(args[1])); } } public static String encrypt(String plainText) { String s = plainText.toLowerCase();// Taking out upper cases s = s.replaceAll("\\s","");//Taking out blank spaces char [] Arr = s.toCharArray();//Convert from string to array of characters float squareRoot = (float) Math.sqrt(s.length()); int dimension = (int) Math.ceil(squareRoot); int x = dimension; char[][] box = new char [x][x];// This is the box: a 2 dimensional array int counter = 0; for(int i = 0; i<x; i++){ for(int j = 0; j<x; j++){ box[i][j]= Arr[counter]; counter++; } } System.out.print(box); return s; } }
Errors:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
at CaesarBox.encrypt(CaesarBox.java:39)
at CaesarBox.main(CaesarBox.java:11)