0

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
3
  • Why don't you simply use String[][] for your board, since you insist on it being a String? Also, what didn't work? Figured out why it didn't work? Commented Apr 10, 2018 at 13:05
  • 1
    2 dimensional array is much better in this situation. You can access it by using array[0][0] = "0" Commented Apr 10, 2018 at 13:07
  • I have never worked with 2d arrays could you provide an answer of how i could achieve this? Commented Apr 10, 2018 at 13:09

4 Answers 4

1

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

This is what I'm looking for I never worked with 2d arrays Thanks m8!
How can StringTokenize a 2d array, i have tried this but it doesnt work for (int i = 0; i < WIDTH_EASY; ++i) { StringTokenizer tokenizer = new StringTokenizer (debug_board_state[i][i], ",");
To form a matrix, if i try to print them without println(); they are printing in line i need matrix to be in column row format rather than in line if you get me
So what could i use to achive this?
I need to other stuff to the matrix that's why i thought i needed to StringTokenize the matrix in order to have it in a column row format without printing it out tbh i dont raeally need to print the matrix out i need it to be stored in a array vairable
|
0

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

Comments

0

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');

1 Comment

And how could i put it into the array i have above?
0

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.

Comments

Your Answer

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