0

To put a String into a single array is easy but I want to put a string into a 2d char[][]; I'm stuck here, can some one help me please ... Thank u, ans sorry for my bad English!

    String woord = "GPDNATSFASELNIERTPOTSRARIRRCOOFPUAUOGONOTORENOTUAMRHRILGTPOFRSCENOIEKLMETANTRSRUNIAARSETEITNAKAVERNTEJLIBFTNVOTWEEDEKLASC";

    char[][] bord = new char[11][11];
    char[] letters = woord.toCharArray();
    int teller = 0;
    //Board into a single array
            for (int i = 0; i < woord.length(); i++) {
                letters[i] = woord.charAt(i);
                teller++;
                System.out.print(letters[i]);
                if (teller % 11 == 0) {
                    System.out.println();
                }
            }     
    //Board into a 2d Array
    for (int r = 0; r < bord.length; r++) {
         bord[r][0]=letters[r];   //<=== first 11 letters, next?
         System.out.print(bord[r][0]);
         for (int c = 0; c < bord[0].length; c++) {
              //??
         }
    }
1
  • Welcome to SO. Please indicate the language tag that is related to your question. I've added java for your question. Commented Feb 5, 2014 at 16:04

1 Answer 1

1

You can use usual trick applied for multi-dimensional arrays while traversing it. r*11 + a value (according to loops) will give us the next character of string. Below code,

//Board into a 2d Array
for (int r = 0; r < bord.length; r++) {
     for(int a = 0; a < 11; a++)
         bord[r][a] = letters[r*11 + a];

     System.out.println(bord[r]);
}

will give the output:

GPDNATSFASE
LNIERTPOTSR
ARIRRCOOFPU
AUOGONOTORE
NOTUAMRHRIL
GTPOFRSCENO
IEKLMETANTR
SRUNIAARSET
EITNAKAVERN
TEJLIBFTNVO
TWEEDEKLASC
Sign up to request clarification or add additional context in comments.

Comments

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.