2

As the title say i want to populate a 2d array with random strings, in this case "_" and "W", and i made both the 2d array and a random string generator but i can find a away to populate the array created with the string generated

import java.security.SecureRandom;

public class Array_Generator {
public static void main(String[] args) {

    int row = 5;
    int col = 5;
    int[][] grid = new int[row][col];
    String AB = "_W";
    SecureRandom rnd = new SecureRandom();
    for( int i = 0; i < grid.length; i++ ) {
        StringBuilder sb = new StringBuilder(row);
        sb.append( AB.charAt( rnd.nextInt(AB.length()) ) );
        sb.toString();


    }
System.out.println(sb);

If i do this i get the row like i want, but i cant find a away to do the next ones.

Any help would be apreciated.

1
  • So what are you doing with sb.toString()? You're iterating over a 2d array of ints, but not putting anything in there, and your question is asking about a 2d array of string. Are you printing out the string somewhere? Commented Jun 2, 2017 at 17:08

3 Answers 3

2

grid is an int[][] so it cannot hold Strings. Change the type, and in the inner loop assign the random String.

// int[][] grid = new int[row][col];
String[][] grid = new String[row][col];

...

// sb.toString();
grid[i][j] = sb.toString();
Sign up to request clarification or add additional context in comments.

Comments

1

Here is one of the ways:

import java.security.SecureRandom;

public class Array_Generator {

    public static void main(String[] args) {

        int row = 5;
        int col = 5;
        String[][] grid = new String[row][col];
        String AB = "_W";
        SecureRandom rnd = new SecureRandom();
        for (int i = 0; i < grid.length; i++) {
            StringBuilder sb = new StringBuilder(row);
            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();
        }

    }
}

OUTPUT:

WW_W__W__WW__W_
__W_WW_WW__WW_W
__W_W__W_W_W_W_
WW_W_WW_W_W_W__
WW_W__W___W____

2 Comments

your answer is also correct and i will mark it as the correct one since its more complete thank you.
Me too, thank you! Have a nice time for programming!
0

You have used two dimensional(2D) array. Because of that you need to iterate through the both array. As a solution you can use nested for loop.

  • First change to 2D array type to String.
  • Next add sb.toString(); to that array.

Then:

for(int i = 0; i < grid.length; i++){
    for(int y = 0; y < grid[i].length; y++){
        System.out.println(i + " ");
        System.out.print(y + " ");
        //Or - System.out.print(grid[i][y] + " ");
    }
}

Or you can use for-each:

for(String[] i : grid){
    for(String y : i){
        System.out.print(y + " ");
    }
    System.out.println();
}

Comments

Your Answer

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