1

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

3
  • 3
    it looks like you need a rubber_duck Commented Jun 14, 2017 at 15:52
  • 1
    "but 10 columns, why is that?" <- Because you have a 2d array of Strings and some of those string are longer that just 1 character. You get more than 1 character per string because you keep appending to your StringBuilder sb inside the inner loop without resetting it. Why do you use a StringBuilder anyway? Edit: Since you don't clear your StringBuilder you grid[x][0] will have 1 character in it, grid[x][1] contains 2 characters, grid[x][2] 3 character and so on. Commented Jun 14, 2017 at 15:57
  • Ah yeah you're right, I'll look into it Commented Jun 14, 2017 at 16:01

1 Answer 1

1

Your problem is that you don't clear the StringBuilder. in the inner loop. so first time you write 1 char, than 2 chars and 3 and 4 (which totals to 10)

You can fix it like that:

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.setLenght(0); // <-- To reset the data
        sb.append(AB.charAt(rnd.nextInt(AB.length())));
        sb.toString(); // You can delete this line which does nothing.
        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();
}

But I would avoid the SB at all... and with the S and E

String[][] grid = new String[int1][int2];
String AB = "_W";
SecureRandom rnd = new SecureRandom();
for (int i = 0; i < grid.length; i++) {
    for (int j = 0; j < grid[i].length; j++) {
        grid[i][j] = "" + AB.charAt(rnd.nextInt(AB.length()));
    }
}

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();
}

grid[0][rnd.nextInt(int2)] = "S";
grid[int1-1][rnd.nextInt(int2)] = "E";
Sign up to request clarification or add additional context in comments.

4 Comments

What about the S and E? Do you recommend using a replace method? But yeah thanks, it works!
(1) Which S and E ? by SB I meant the StringBuilder. (2) I'm not sure which replace method you mean. (3) If it works, please accept the answer.
I mean the last paragraph that I wrote. " 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'."
I add also 2 lines in the end to handle the S and the E.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.