Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with 2D char array

918375Apr 14 2012 — edited Apr 14 2012
Hello All,

I am attempting to create a very simple Boggle game using a 2D array. I have successfully created an array with random chars, but would like to statically throw some words in the array to search for. My thought process was to fill the array with null values, set some words, and fill the null indexes with the random chars. If I try to set grid[0][0] = 'c', I receive the following error:

wordSearch.java:25: incompatible types
found : char
required: wordSearch.letters grid[0][0] = 'c';

I would greatly appreciate any suggestions and help! Thanks in advance! Also, please bear with my novice code... :)

import java.util.*;

public class wordSearch {
public enum letters {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z};
Random r = new Random();
private letters[][] grid;

/***************
* CONSTRUCTOR *
***************/

public static void main(String[] args) {
wordSearch WS = new wordSearch(20);
}

public wordSearch(int size) {
grid = new letters[size][size];

// fill grid with null values
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
grid[row][col] = getRandomLetter();
}
}

// fill the grid with random letters if space == null
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
// grid[row][col] = getRandomLetter();
// grid[row][col] = null;
do {
grid[row][col] = getRandomLetter();
}
while (grid[row][col] == null);
}
}

// display grid's output
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}

// Method to generate random letter
private letters getRandomLetter() {
letters[] options = letters.values();
int length = options.length;
int index = r.nextInt(length);
return options[index];
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 12 2012
Added on Apr 14 2012
5 comments
1,023 views