0

So I have an arrayList of Strings imputed by the user.I also have a 2d array of chars : private char[][] puzzle ; I need help changing the Strings to characters so I can enter them into the 2d array can anyone help??

public class WordSearchPuzzle
{
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;
    private int letterCount = 0 ;
    private int gridDimensions;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    private void createPuzzleGrid()
    {
        int i;
        for(i = 0; i < puzzleWords.size() ; i++){
            letterCount = puzzleWords.size() + letterCount ;
        }
        gridDimensions = letterCount * 2;
        puzzle = new char[gridDimensions][gridDimensions] ;
    }

3 Answers 3

1

Change each string to a charArray.

for(String item : ArrayList){
  item.toCharArray();
}
Sign up to request clarification or add additional context in comments.

2 Comments

will that put the array of chars to an array or an arrayList
@user1323808 If you look at the documentation it states Returns: a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
0

Tagged as homework. Like in your previous questions, I'll ask: did you try anything yet? How do you plan to solve this? What is your current problem?

I'll give you an idea. createPuzzleGrid() method inialises the puzzle array for you. Note how they calculate the dimensions from the list of words. This should point you in the right direction: how to copy characters from the list of words into the array.

Comments

0
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WordSearchPuzzle {

    private char[][] puzzle ;
    private List<String> puzzleWords; //note: don't use a specific implementation (e.g. ArrayList) when you define a List  

    public WordSearchPuzzle(List<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    public void createPuzzleGrid()
    {
        puzzle = new char[puzzleWords.size()][];
        for(int i = 0; i < puzzleWords.size() ; i++){
            puzzle[i] = puzzleWords.get(i).toCharArray();
        }
    }   


    public void debugPuzzle(){
        System.out.println(Arrays.deepToString(puzzle));
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        List<String> userWords = new ArrayList<String>();
        userWords.add("word");
        userWords.add("longlongword");
        userWords.add("hello");

        WordSearchPuzzle puzzle = new WordSearchPuzzle(userWords);
        puzzle.createPuzzleGrid();
        puzzle.debugPuzzle();
    }

}

result

[[w, o, r, d], [l, o, n, g, l, o, n, g, w, o, r, d], [h, e, l, l, o]]

4 Comments

will that make a difference to the size of the 2d array.I want to keep it to the length of the characters in the arrayList * 2 so here is my code for that:private void createPuzzleGrid() { int i, itemLength; String item; for (i = 0; i < puzzleWords.size(); i++) { item = puzzleWords.get(i); itemLength = item.length(); letterCount = letterCount + itemLength; } gridDimensions = letterCount * 2; puzzle = new char[gridDimensions][gridDimensions] ; }
OK, I understand, that this result does not fit for you: [[w, o, r, d], [l, o, n, g, l, o, n, g, w, o, r, d], [h, e, l, l, o]]. But I still don't get what are you trying to achieve? What is your purpose with puzzle[][]? I'm not sure char[letterCount * 2][letterCount * 2] is useful, maybe you need char[maxWordLenght][maxWordLenght]?
Thanks for the reply ok what im trying to achieve is a word search puzzle using a 2d array where the user inputs a arrayList of Strings and we have to convert the strings to chars because the 2d array will only accept chars and then enter the words(as chars) randomly into the 2d array.Thanks a million for the help!If you have skype you can add me to chat easier!
I see. How would you define the size of the 2d array, in which you will try to put words randomly? (I mean in layman's terms)

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.