So I'm developing a simple program that asks the user how many columns and lines it wants, and then generates a string with the afformentioned size, here's the code (adapted from what some kind developer made to answer another question)
String[][] grid = new String[int1][int2];
String AB = "_W";
SecureRandom rnd = new SecureRandom();
for (int i = 0; i < grid.length; i++) {
StringBuilder sb = new StringBuilder(int1);
for (int j = 0; j < grid[i].length; j++) {
sb.append(AB.charAt(rnd.nextInt(AB.length())));
sb.toString();
grid[i][j] = sb.toString();
}
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
System.out.print(""+grid[i][j]);
}
System.out.println();
}
}
Now, this has a bug I can't seem to fix, I collect the wanted size through int1 and int2 (int1 being rows and int2 being columns), however the output respects the number of rows (int2) but doesn't seem to respect the column number. Here's an example, if I define int1 (rows) = 3 and int2 (columns) = 4 here's the output:
_____W__W_
_____W__W_
_____W__WW
As you can see, there's 3 rows but 10 columns, why is that? How do I fix it?
I also need to implement a simple addon that I can't seem to get my head around. In the first row, there needs to be (in a random position, just has to be the first row) the char 'S' and in the last row there needs to be the char 'E' (also in a random position). They can't add to the size of their respective columns, so they need to replace a '_' or 'W'. Any help? :D