Click to See Complete Forum and Search --> : 'Crossword' Program


eyeoforion
04-12-2005, 12:11 AM
Ok here's the rub
The idea is to create a program which will extract words from a 'crossword' aka an array using stringbuffer, convert into a string and extract each word and print it without any trailing white space either side. Here's the array, any ideas on how to accomplish it?

public class crossword {
public static void main(String[] args) {

char[][] arrayOfChars = { { ' ',' ','J', 'A', 'V', 'A' },
{ 'I','S','', ' ', ' ', ' ' },
{ ' ',' ','L', 'O', 'T', 'S' },
{ ' ','O','F', ' ', ' ', ' ' },
{ ' ',' ','F', 'U', 'N', ' ' },
};

What I've worked out so far is the program will need to traverse the array pulling out its elements and putting them into a stringbuffer, going to require some sort of loop and a way to distinguish rows and columns.
The stringbuffer will then have to be converted to a string
Then the program will need to get rid of the spaces either side of the letters, using string.trim maybe.
Then somehow get each word out seperately and print it. My order may be a bit screwed but you get the generaly gist I hope.

Thanks in advance for any help, Orion

7stud
04-16-2005, 07:12 AM
Here's the array, any ideas on how to accomplish it?
char[][] arrayOfChars = {
{ ' ', ' ', 'J', 'A', 'V', 'A' },
{ 'I', 'S', ' ', ' ', ' ', ' ' },
{ ' ', ' ', 'L', 'O', 'T', 'S' },
{ ' ', 'O', 'F', ' ', ' ', ' ' },
{ ' ', ' ', 'F', 'U', 'N', ' ' },
};

Yep. Here is my output:
rows:

JAVA
IS
LOTS
OF
FUN

columns:
I
SO
JLFF
AOU
VTN
AS
The way to do that is:

1) Create two StringBuffer arrays: one for the words in the rows and one for the words in the columns. The size of 'rows[]' will be arrayOfChars.length, which is the number of 1d arrays inside arrayOfChars. The size of 'columns[]' will be the length of any of the 1d arrays, e.g. arrayOfChars[0].length. Using the StringBuffer method append(), you can append any type to the StringBuffer, including chars. Try to successfully append some chars to your StringBuffer arrays before proceeding. There is an issue you will have to deal with.

2) Then, you just need to cycle through each row and each char in each row with two nested for loops--standard operating procedure for a 2d array. The outer loop will also be the index value for your rows[] StringBuffer array. That way, as you move from row to row in the arrayOfChars, you will move from row to row in your rows[] array. Using the append() method, you can add chars to rows[outerIndex]. However, you don't want to add every char, you only want to add chars if the chars are != ' ', which you can check with a simple if statement.

3)You can do the columns the same way. But in this case, the outer for loop will hold the column index constant as you increment down the rows with the inner for loop. Then, the outer loop will increment and move to the next column, and you will step down through the rows again. Once again, you only add the char to columns[outerIndex] if it is not equal to a space.

After those steps, your rows[] and columns[] StringBuffer arrays will hold the words found in arrayOfChars[] minus any spaces. To display them, you just need to use a for loop. I'm not sure why you need to convert the StringBuffers to Strings, but if you need to do that, then use the StringBuffer toString() method:

StringBuffer strBuff = new StringBuffer("some text");
String str = strBuff.toString();