user8580441
user8580441

Reputation:

How to randomly insert String numbers into an array-Java

I have this array here that takes strings values this is my Puzzle Board View, Right now everything works but the array is hard coded and i need the strings to be generated randomly from 0-4. I have tried to get a random char and put it is as a string but this didn't work. Any tips would be nice.

Random rand = new Random();
char c = (char)(rand.nextInt(5) + '0');
StringBuilder sb = new StringBuilder();
sb.append(c);

String[] debug_board_state  = new String[7];
debug_board_state[0] = "0,3,0,0,3,0,2";
debug_board_state[1] = "1,0,2,0,0,1,2";
debug_board_state[2] = "0,2,0,0,0,0,0";
debug_board_state[3] = "0,0,3,0,3,0,4";
debug_board_state[4] = "2,0,0,0,0,1,0";
debug_board_state[5] = "0,1,0,0,1,0,2";
debug_board_state[6] = "2,0,3,0,0,2,0"; 

UPDATE.

Thanks to user Answer i was able to get the random matrix, although i ran into another problem, I need do more stuff to the matrix so i don't want to print it out. here is the code

static private final int WIDTH_EASY = 7;
 protected void InitializeEasy() {
      Random rand = new Random();

      String[][] debug_board_state  = new String[7][7];
      for (int row = 0; row < debug_board_state.length; row++) {
          for (int column = 0; column < debug_board_state[row].length; column++) {
              debug_board_state[row][column] = String.valueOf(rand.nextInt(5));
          }
      }

      for (int row = 0; row < debug_board_state.length; row++) {
          for (int column = 0; column < debug_board_state[row].length; column++) {
              System.out.print(debug_board_state[row][column] + " ");
          }
      };

    for (int i = 0; i < WIDTH_EASY; ++i) {
      StringTokenizer tokenizer = new StringTokenizer (debug_board_state[i][i], ",");
      int column = 0;
      while(tokenizer.hasMoreTokens()) {
    String token = tokenizer.nextToken();
    getCurrentState().board_elements[i][column] = new BoardElement();
    getCurrentState().board_elements[i][column].max_connecting_bridges = Integer.parseInt(token);
    getCurrentState().board_elements[i][column].row = i;
    getCurrentState().board_elements[i][column].col = column;

    if (getCurrentState().board_elements[i][column].max_connecting_bridges > 0) {
      getCurrentState().board_elements[i][column].is_island = true;
    }
    ++column;
      }
    }
  }

The string Tokenizer works with 1d array but not with 2d, i need something that will do the same thing as StringTokenizer and apply it to the matrix. I am getting the following error

java.lang.NullPointerException: Attempt to read from field Island_and_Bridges.Hashi.BoardElement[][] Island_and_Bridges.Hashi.BoardState$State.board_elements on a null object reference

Upvotes: 0

Views: 669

Answers (4)

xingbin
xingbin

Reputation: 28279

Although I think int[][] is a better idea, here is the String[][] solution. You can use String.valueOf(rand.nextInt(5)) to generate element in the matrix:

import java.util.Random;

public class Main {

    public static void main(String[] args) {
        Random rand = new Random();

        String[][] matrix  = new String[7][7];
        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix[row].length; column++) {
                matrix[row][column] = String.valueOf(rand.nextInt(5));
            }
        }

        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix[row].length; column++) {
                System.out.print(matrix[row][column] + " ");
            }
            System.out.println();
        }
    }
}

Update:

for (int row = 0; row < WIDTH_EASY; ++row) {
    for (int column = 0; column < WIDTH_EASY; ++column) {
        getCurrentState().board_elements[row][column] = new BoardElement();
        getCurrentState().board_elements[row][column].max_connecting_bridges = Integer.parseInt(debug_board_state[row][column]);
        getCurrentState().board_elements[row][column].row = row;
        getCurrentState().board_elements[row][column].col = column;
        if (getCurrentState().board_elements[row][column].max_connecting_bridges > 0) {
            getCurrentState().board_elements[row][column].is_island = true;
        }
    }
}

Upvotes: 1

M_I
M_I

Reputation: 132

Maybe, you want something like this:

public void initBoard() {
    Random random = new Random();
    String[][] board = new String[7][7];
    for (int i=0; i < board.size(); i++) {
        for (int j=0; j < board[].size(); j++) {
            board[i][j] = String.valueOf(random.nextInt() % 5);
        }
    }
}

It will initialize your board with random number of String.

Upvotes: 0

Ninja
Ninja

Reputation: 205

0-4 lies from 49 to 52 on the ASCII scale:

Random rand = new Random();
char c = (char)(rand.nextInt(4)+49);
StringBuilder sb = new StringBuilder();
sb.append(c+'0');

Upvotes: 0

Sebastian Karlsson
Sebastian Karlsson

Reputation: 745

Something like this?

Pseudo-code:

String[][] debug_board_state  = new String[7][7];

for (int x = 0; x < debug_board_state.size(); x++) {
     for (int y = 0; y < debug_board_state[x].size(); y++) {
         debug_board_state[x][y] = new_random_character();
     }
}

Upvotes: 0

Related Questions