Hi there, I'm attempting to write a program that does the target word puzzles you see in most major newspapers. The target word problem involves a 3x3 grid of 9 random letters that a user needs to be able to make words out of using at least the middle letter and 3 others. No letter can be repeated when making the word but there can be more than the one of the same letter in the grid. First up i'm trying to create the grids using two different arrays, one with the whole alphabet and an empty array where 9 letters will be randomly chosen. I haven't even been able to do this let alone think about the next part of figuring out which words are valid. Here is my code so far...
import java.util.*;

public class  array
	{

		public static void main(String[] args)
			{

				String a[] = {"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",};
				String[] b;


			for (int i=0; i<a.length; i++)

				{
					System.out.println(a[i]);
				}

			Random r = new Random();

				for (int i=0; i<8; i++)
				{
					String randomChar = a.charAt(r.nextInt(i));

					System.out.println();

				}


			}
	}

Any suggestions/direction or advice about it would be greatley appeciated.

P.S. I don't need it to have a GUI

Recommended Answers

All 5 Replies

I've managed to find two nice seperate bits of code that i'm trying to combine together to get my desired result. The first bit is....

import java.io.IOException;

public class gaelan {
  static int size;

  static int count;

  static char[] charArray;

  public static void main(String[] args) throws IOException {

    String input = "Java Source and Support";
    size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
      charArray[j] = input.charAt(j);
    doAnagram(size);
  }

  public static void doAnagram(int newSize) {
    int limit;
    if (newSize == 1) // if too small, return;
      return;
    // for each position,
    for (int i = 0; i < newSize; i++) {
      doAnagram(newSize - 1); // anagram remaining
      if (newSize == 2) // if innermost,
        display();
      rotate(newSize); // rotate word
    }
  }

  // rotate left all chars from position to end
  public static void rotate(int newSize) {
    int i;
    int position = size - newSize;
    // save first letter
    char temp = charArray[position];
    //shift others left
    for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
    //put first on right
    charArray[i - 1] = temp;
  }

  public static void display() {
    System.out.print(++count + " ");
    for (int i = 0; i < size; i++)
      System.out.print(charArray[i]);
    System.out.println();
  }
}

The second bit of code is...

import java.util.*;

public class  ar
{
	public static void main(String[] args) 
	{
		String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		Random r = new Random();
			
			for(int i=1; i<10; i++)
			{    char randomChar = validChars.charAt(r.nextInt(validChars.length()));
				 System.out.println(randomChar);
			}
			

	}
}

Both bits of code work well seperately but when I try and substitute the

String input = "Java Source and Support";

for something like this

String input = randomChar;

The compiler yells at me. I know this solution doesn't address the small combinations but its the only bit of progress i've got so far...

So if anyone out there has any ideas what im doing wrong and how to combine these two, it would be awesome if you could post a reply.

OK so now I've realised i needed to create another array to play the randomChars produce in the loop...

So say put this with the other Array...

static char[] oneArray;

Now i'm not sure how to add the random elements produced by...

for(int i=1; i<10; i++)			{    char randomChar = validChars.charAt(r.nextInt(validChars.length()));				 System.out.println(randomChar);			}

And whether making... the following would actually work?

String input = oneArray

Any input anyone?

Are you trying to add values in to oneArray?

Are you trying to add values in to oneArray?

Yes I'm not sure how to code the values into oneArray? Also whether this will allow for the two parts to work together by putting it into..

String input = oneArray;

Which would hopefully allow the letters to be sorted for all combinations?

Hi I figured the code for this it is placed below if anyone wants to see the final product...

import java.io.*;
import java.util.*;
import java.lang.*;

public class program3

{
  static int size;
  static int count;
  static char[] charArray;
  static Scanner in;

  public static void main(String[] args) throws IOException
  {

	in = new Scanner(System.in);

	String s1= null;
	String temp = "";
	String wait = "";

	String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	Random r = new Random();


	for(int i=0; i<9; i++)
			  {
				  char randomChar = validChars.charAt(r.nextInt(validChars.length()));
			      s1 = Character.toString(randomChar);
				  temp = s1 + temp;
				  System.out.print(s1);		
			  }
	

	System.out.println("\nPress Y then enter to continue");		    
	wait = in.next();
	System.out.println(temp);
	String input = temp;
   
	size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
   	  charArray[j] = input.charAt(j);
    doAnagram(size);
	
  }

  public static void doAnagram(int newSize)
  {
    int limit;
    if (newSize == 1) // if too small, return;
      return;
    // for each position,
    for (int i = 0; i < newSize; i++) {
      doAnagram(newSize - 1); // anagram remaining
      if (newSize == 2) // if innermost,
        display();
      rotate(newSize); // rotate word
    }
 }

  // rotate left all chars from position to end
  public static void rotate(int newSize)
  {
    int i;
    int position = size - newSize;
    // save first letter
    char temp = charArray[position];
    //shift others left
    for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
    //put first on right
    charArray[i - 1] = temp;
  }

  public static void display()
  {
    System.out.print(++count + " ");
    for (int i = 0; i < size; i++)
      System.out.print(charArray[i]);
    System.out.println();
  }
}import java.io.*;
import java.util.*;
import java.lang.*;

public class program3

{
  static int size;
  static int count;
  static char[] charArray;
  static Scanner in;

  public static void main(String[] args) throws IOException
  {

	in = new Scanner(System.in);

	String s1= null;
	String temp = "";
	String wait = "";

	String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	Random r = new Random();


	for(int i=0; i<9; i++)
			  {
				  char randomChar = validChars.charAt(r.nextInt(validChars.length()));
			      s1 = Character.toString(randomChar);
				  temp = s1 + temp;
				  System.out.print(s1);		
			  }
	

	System.out.println("\nPress Y then enter to continue");		    
	wait = in.next();
	System.out.println(temp);
	String input = temp;
   
	size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
   	  charArray[j] = input.charAt(j);
    doAnagram(size);
	
  }

  public static void doAnagram(int newSize)
  {
    int limit;
    if (newSize == 1) // if too small, return;
      return;
    // for each position,
    for (int i = 0; i < newSize; i++) {
      doAnagram(newSize - 1); // anagram remaining
      if (newSize == 2) // if innermost,
        display();
      rotate(newSize); // rotate word
    }
 }

  // rotate left all chars from position to end
  public static void rotate(int newSize)
  {
    int i;
    int position = size - newSize;
    // save first letter
    char temp = charArray[position];
    //shift others left
    for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
    //put first on right
    charArray[i - 1] = temp;
  }

  public static void display()
  {
    System.out.print(++count + " ");
    for (int i = 0; i < size; i++)
      System.out.print(charArray[i]);
    System.out.println();
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.