3

I'm trying to randomly place 1D string array into 2D char array but I'm having issues with my for-loop. userWords is 1D array of String while puzzleBoard is a 2D array of char.

I've tried

for(int i=0; i<userWords.length;i++) {
        puzzleBoard[r++] = userWords[i].toCharArray(); 
    }

but it's not placing it randomly like I want it to

So I tried

    for(int i=0; i<userWords.length;i++) {
        int r = rand.nextInt(ROW) + 1;
        int c = rand.nextInt(COLUMN) + 1;
        puzzleBoard[r][c] = userWords[i].charAt(i);
    }

but it's printing only 3 char instead of the 3 strings of char into the char array.

I've also tried

    puzzleBoard[r][c] = userWords[i].toCharArray();

instead of

    puzzleBoard[r][c] = userWords[i].charAt(i);

But it display error "cannot convert from char[] to char"

Thank you

Full Code

public static void main(String[] args) {
    String[] userWords = new String[3];
    Methods.userInput(userWords); //ask user for input 
    Methods.fillPuzzle(puzzleBoard); //fill the puzzle with random char

    for(int i=0; i<userWords.length;i++) {
        int r = rand.nextInt(ROW) + 1;
        int c = rand.nextInt(COLUMN) + 1;
        puzzleBoard[r][c] = userWords[i].charAt(i);
    }

    Methods.printPuzzle(puzzleBoard); //print out the puzzle

}//end main 


public static void printPuzzle(char a[][]) {

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) {
            System.out.print(a[i][j] + " ");
        }
        System.out.print((i+1));
        System.out.println();
    }

}//end printPuzzle

public static void fillPuzzle(char a[][]) {

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) {
            a[i][j] = '*';
        }
    }
}//end fillPuzzle

public static void userInput(String a[]) {
    Scanner input = new Scanner(System.in);

    for(int i = 0; i < a.length;i++) {
        System.out.println((i+1) + ". enter word:");
        a[i] = input.next().toUpperCase();
    }
}//end userInput
1
  • Can you give full code? Commented Mar 14, 2019 at 6:15

2 Answers 2

3

You can try this one:

  for (int i = 0; i < userWords.length; i++) {
     int r = rand.nextInt(puzzleBoard.length);
     int c = rand.nextInt(puzzleBoard[r].length - userWords[i].length());
     for (int j = 0; j < userWords[i].length(); j++) {
        puzzleBoard[r][c + j] = userWords[i].charAt(j);
     }
  }

And you should add something that detects whether there is already a word at this position, otherwise you would overwrite it if the random numbers point to a location where is already written a word.

Sign up to request clarification or add additional context in comments.

1 Comment

It works! I was having such a hard time, thank you. And yes I'm planning to do that right after
1

I think you should use 2 for-loops because you want to select first the string and next the characters in the string.

    for(int i=0; i<userWords.length;i++) {
       int r = rand.nextInt(ROW) + 1;
       int c = rand.nextInt(COLUMN) + 1;
       for (int j = 0; j < userWords[i].length(); j++) {
          puzzleBoard[r][c + j] = userWords[i].charAt(j);
       }
    }

Comments

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.