This discussion is archived
5 Replies Latest reply: Apr 14, 2012 3:19 PM by 796440 RSS

Help with 2D char array

918375 Newbie
Currently Being Moderated
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];
     }     
}
  • 1. Re: Help with 2D char array
    796440 Guru
    Currently Being Moderated
    The error is telling you exactly what's wrong. You have a 2D array of letters, and your trying to stick a char into it. A char is not a letters.

    It's as if you did this:
    letters x = 'c';
    You wouldn't expect that to work, would you?
  • 2. Re: Help with 2D char array
    918375 Newbie
    Currently Being Moderated
    Thank you for your quick response. I am VERY new to Java and would appreciate a little more help. I changed the array to the following:

    public char[] 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();
         public grid[][] grid = new letters[][];

    And am now getting the error:
    wordSearch.java:6: array dimension missing --> public grid[][] grid = new letters[][];

    If anyone can please help, besides telling me it's wrong, I would really appreciate it!

    Thanks again.
  • 3. Re: Help with 2D char array
    796440 Guru
    Currently Being Moderated
    915372 wrote:
         public grid[][] grid = new letters[][];
    What exactly are you expecting that line to do?

    First you're declaring a 2D array of grids, so you'll have M*N grids. What's a grid, and why do you want an array of them?

    Then you're giving the variable the same name as the class--grid--which leads to confusion.

    Then you create a 2D array of letters, only you don't give it any dimensions, and for it to work to assign it to a grid[][] variable, letters would have to be a subclass of grid.

    It looks like you're just guessing. Don't do that. It's a horribly ineffective way to program. Don't write a single bit of code if you don't know exactly what it does.

    So what do you want? M*N characters? M*N grids? M*N letterses?

    wordSearch.java:6: array dimension missing --> public grid[][] grid = new letters[][];
    Just like before, the error message is telling you exactly what's wrong. You know what "dimension" means in an array context, right? And you've gone through an array tutorial and can compare that to what you wrote and see the difference, right?
  • 4. Re: Help with 2D char array
    918375 Newbie
    Currently Being Moderated
    I am guessing and just trying to have fun. The tutorial is gibberish to me and was just hoping for help. Thanks anyway
  • 5. Re: Help with 2D char array
    796440 Guru
    Currently Being Moderated
    915372 wrote:
    I am guessing and just trying to have fun. The tutorial is gibberish to me and was just hoping for help. Thanks anyway
    Okay then. Good luck.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points