Hi there,
I am new to java and am having trouble with some code... I am trying to input the characters from a string into a 2d array. The array size depends on the amount of characters in the string (get the square root and round up to the nearest int so that the array is a square grid). I'm getting an out of bounds error, but not sure why. I would appreciate any help! Thanks
This is what I have so far:
public class Practice { public static void main(String[] args) { String text = args[0]; text = text.replaceAll("\\s", ""); text = text.toLowerCase(); System.out.println(text); float squareRoot = (float) Math.sqrt(text.length()); int dimension = (int) Math.ceil(squareRoot); int x = dimension; int y = dimension; char[] charArray = text.toCharArray(); char[][] myArray = new char[x][y]; int nextChar = 0; for (int a = 0; a < x; ++a) { for (int b = 0; b <y; ++b) { myArray[x][y] = charArray[nextChar]; nextChar++; } } } }